How do I get a button to open another activity?

前端 未结 8 1026
执笔经年
执笔经年 2020-12-02 13:09

I\'ve added a button to my activity XML file and I can\'t get it to open my other activity. Can some please tell me step by step on how to do this?

8条回答
  •  长情又很酷
    2020-12-02 14:07

    If you declared your button in the xml file similar to this:

    then you can use it to change the activity by putting this at the java file:

    public void goToActivity2 (View view){
        Intent intent = new Intent (this, Main2Activity.class);
        startActivity(intent);
    }
    

    Note that my second activity is called "Main2Activity"

提交回复
热议问题