简体   繁体   中英

Multiple activities and buttons?

I want to make buttons once I click on the button I go to another activity? and the problem is only the first button is working!

public class Main extends Activity {

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

        setContentView(R.layout.main);

        Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
        PageOneButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent myIntent = new Intent(v.getContext(), PageOne.class);
                v.getContext().startActivity(myIntent);

                Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
                PageTwoButton.setOnClickListener(new View.OnClickListener() {   
                    public void onClick(View v) {
                        Intent myIntent = new Intent(v.getContext(), PageTwo.class);
                        v.getContext().startActivity(myIntent); 
                    }
                    {}
                });
            }
        });
    }
}

Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.

Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){


    public void onClick(View v) {
        Intent myIntent = new Intent(Main.this, PageOne.class);
        startActivity(myIntent);

});


Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v) {
        Intent myIntent = new Intent(Main.this, PageTwo.class);
        startActivity(myIntent);

});

Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.

Edit:

Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.

Hope it makes sense, nevertheless the code above should fix the issue.

There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:

Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);    

private OnClickListener addGroupOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        if (v == PageOneButton) {
            Intent myIntent = new Intent(Main.this, PageOne.class);
            startActivity(myIntent);
        } else if (v == PageTwoButton) {
            Intent myIntent = new Intent(Main.this, PageTwo.class);
            startActivity(myIntent);
        }  
   }
};

Hope this helps!

Two words:

Code Indentation

Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.

There's also an extra pair of {}, which is redundant.

Also, @edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM