0

我有一个 Android 应用程序,它需要一个按钮来打开一个新的 xml 页面。这就是现在的样子,有人可以添加必要的代码以在我单击按钮时打开 Page2Activity 吗?代码:

    public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Void onClick;View arg0; {
        // TODO Auto-generated method stub

    }
    };

    }

我使用这种方法解决了这个问题:http ://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,但我也会尝试你的所有方法。

4

2 回答 2

2

试试这个代码:

public void handleClick(View v){
    //Create an intent to start the new activity.
    Intent intent = new Intent();
    intent.setClass(this,Page2Activity.class);
    startActivity(intent);
}

然后创建一个名为Page2Activity.

希望这会有所帮助,不要忘记将您的活动添加到清单文件中。

于 2012-04-25T00:58:25.560 回答
1

我想你的意思是这样的:

public class MyClass extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent("com.myaction");
                startActivity(i);
            }
        });
    }
}
于 2012-04-25T11:11:37.043 回答