简体   繁体   中英

When clicking the button, the content doesn't change. - Android

I want to change the content on a button change, but it never works.

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.blahdyblah);
            }
        });

So that's the code, but whenever I want to change setContentView(), it doesn't change, it just clicks and does nothing. If anyone could help me in this task...

I've also tried putting the setContentView in another function... That still doesn't work.

Can you try ActivityName.this.setContentView(R.layout.blahdyblah) where ActivityName is your activity? It could be that setContentView is called on the context you are currently dealing with.. which inside your button onclick is the button

If that does not work could you please edit your answer to display your entire code

I would suggest; 1) make sure the button is in the same context / class if where your current view is 2) make sure your 'blahdyblah' is a properly set up XML file

You may also want to start a new activity, in which you can setContentView -- perhaps there is a problem changing the content in your current activity.

Do it this way:

{
    ...
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            setBlahdyBlah();
        }
    });
}

...

private void setBlahdyBlah() {
    setContentView(R.layout.blahdyblah);
}

This will guarantee that your call to setContentView() is executed from the right context.

I'm not exactly sure why it didn't work the way you wrote it, but I think your code depends on your OnClickListener having been created as a proper inner class of the Activity, and that the global 'R' be correctly accessible from that scope. I'm not sure where it failed, but my way eliminates the unknowns from this problem. Plus, that's how I did it and it worked for me.

Hmmm, upon further consideration, I bet this would work too:

    final Activity foo = this;
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            foo.setContentView(R.layout.blahdyblah);
        }
    });

I think it's all related to your OnClickListener not being a proper inner class of your activity.

I'm guessing you want your screen to switch to another layout when the button is clicked. Try the following. First of all, make an activity called "blahdyblah" instead of just an xml file(So you should have two activities, your main activity which has the button with the onClickListener, and a separate activity called "blahdyblah"). After you've done that, try this:

button.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(MainActivity.this, blahdyblah.class);
            startActivity(intent);
        }
});

In the code, replace "MainActivity" with the name of the class in which you are writing this onClickListener.

Set the blahdyblah.java file's content view to R.layout.blahdyblah I'm pretty sure that should do what you're looking for.

blahdyblah.java

public class blahdyblah extends Activity
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blahdyblah);
    }
}

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