How to start new activity on button click

后端 未结 24 1608
傲寒
傲寒 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:37

    Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the Intent class; Intent | Android Developers.

    1. Using the onClick attribute of the Button. (Beginner)
    2. Assigning an OnClickListener() via an anonymous class. (Intermediate)
    3. Activity wide interface method using the switch statement. (Pro)

    Here's the link to my example if you want to follow along:

    1. Using the onClick attribute of the Button. (Beginner)

    Buttons have an onClick attribute that is found within the .xml file:

    In Java class:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }
    
    public void goToAnActivity(View view) {
        Intent intent = new Intent(this, AnActivity.class);
        startActivity(intent);
    }
    
    public void goToAnotherActivity(View view) {
        Intent intent = new Intent(this, AnotherActivity.class);
        startActivity(intent);
    }
    

    Advantage: Easy to make on the fly, modular, and can easily set multiple onClicks to the same intent.

    Disadvantage: Difficult readability when reviewing.

    2. Assigning an OnClickListener() via an anonymous class. (Intermediate)

    This is when you set a separate setOnClickListener() to each button and override each onClick() with its own intent.

    In Java class:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
    
            Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), AnActivity.class);
                    view.getContext().startActivity(intent);}
                });
    
            Button button2 = (Button) findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), AnotherActivity.class);
                    view.getContext().startActivity(intent);}
                });
    

    Advantage: Easy to make on the fly.

    Disadvantage: There will be a lot of anonymous classes which will make readability difficult when reviewing.

    3. Activity wide interface method using the switch statement. (Pro)

    This is when you use a switch statement for your buttons within the onClick() method to manage all the Activity's buttons.

    In Java class:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                Intent intent1 = new Intent(this, AnActivity.class);
                startActivity(intent1);
                break;
            case R.id.button2:
                Intent intent2 = new Intent(this, AnotherActivity.class);
                startActivity(intent2);
                break;
            default:
                break;
        }
    

    Advantage: Easy button management because all button intents are registered in a single onClick() method


    For the second part of the question, passing data, please see How do I pass data between Activities in Android application?

提交回复
热议问题