1

我的项目非常简单,仅用于培训目的。当我单击主布局上的按钮时,我正在尝试加载另一个界面/视图/活动。这是主要活动的代码:

包 com.example.interfacemanipulation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

    public static final String EXTRA_MESSAGE = "Something Here....";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void goToWelcomeActivityMethod(View view)
    {
        Intent intent = new Intent(this, WelcomeActivity.class);
        EditText editText = (EditText) findViewById(R.id.welcomeMessage);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

这是另一个活动中的代码:

package com.example.interfacemanipulation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class WelcomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.welcome, menu);
        return true;
    }

}

这里的代码基本上是从谷歌文档修改而来的。我只想能够加载带有标签的第二个活动的视图。这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.interfacemanipulation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.interfacemanipulation.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.interfacemanipulation.WelcomeActivity"
            android:label="@string/title_activity_welcome" >
        </activity>
    </application>

</manifest>

我最近 4 天才尝试使用 android...所以感谢您的支持

4

2 回答 2

2

您有 2 个问题: 1. 您没有将您带到活动的按钮(或者至少您没有在您发布的代码中显示它。如果您这样做了,那么您没有相应的onClickListener设置按钮)。2. 你没有调用将你带到第二个活动的方法。

要拥有一个将您带到另一个活动的按钮,您需要将该按钮添加到布局中,然后添加,它会在您单击它时onClickListener执行您的操作,如下所示:goToWelcomeActivityMethod

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

    public static final String EXTRA_MESSAGE = "Something Here....";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //this will insert the button into the Main Activity layout
        Button goToWelcome = (Button)findViewById(R.id.goToWelcome);

        //this is the onClickListener which will call the method to go to the next activity
        goToWelcome.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v)
        {
           goToWelcomeActivityMethod();
        }
         });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    //you don't need to pass in the View
    public void goToWelcomeActivityMethod()
    {
        Intent intent = new Intent(this, WelcomeActivity.class);
        EditText editText = (EditText) findViewById(R.id.welcomeMessage);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

然后在你的 XML 文件中确保你也添加了按钮:

 <Button
        android:id="@+id/goToWelcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Go To Welcome" />
于 2013-07-03T18:09:10.503 回答
0

在主活动的布局 XML 中,将属性添加android:onClick="goToWelcomeActivityMethod"到按钮声明中。这样,goToWelcomeActivityMethod当您按下按钮时,就会调用该方法。

对于“其他”方式,请检查https://developer.android.com/reference/android/widget/Button.html

于 2013-07-03T17:50:05.783 回答