How to start new activity on button click

后端 未结 24 1673
傲寒
傲寒 2020-11-21 05:54

In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?

24条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 06:34

    Write the code in your first activity .

    button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
    
    
    Intent intent = new Intent(MainActivity.this, SecondAcitvity.class);
                           //You can use String ,arraylist ,integer ,float and all data type.
                           intent.putExtra("Key","value");
                           startActivity(intent);
                            finish();
                }
             });
    

    In secondActivity.class

    String name = getIntent().getStringExtra("Key");
    

提交回复
热议问题