简体   繁体   English

单击该按钮时,内容不会更改。 -安卓

[英]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. 这就是代码,但是每当我想更改setContentView()时,它都不会改变,它只是单击而不会执行任何操作。 If anyone could help me in this task... 如果有人可以帮助我完成这项任务...

I've also tried putting the setContentView in another function... That still doesn't work. 我也尝试过将setContentView放入另一个函数中...仍然不起作用。

Can you try ActivityName.this.setContentView(R.layout.blahdyblah) where ActivityName is your activity? 您可以尝试在其中使用ActivityName的ActivityName.this.setContentView(R.layout.blahdyblah)吗? It could be that setContentView is called on the context you are currently dealing with.. which inside your button onclick is the button 可能是在您当前正在处理的上下文上调用了setContentView。

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 1)如果当前视图所在的位置,请确保按钮在相同的上下文/类中2)确保“ blahdyblah”是正确设置的XML文件

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. 您可能还想开始一个新的活动,可以在其中设置setView-更改当前活动中的内容可能存在问题。

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. 这将确保您从正确的上下文中执行对setContentView()的调用。

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. 我不完全确定为什么它不能按照您的编写方式工作,但是我认为您的代码取决于将OnClickListener创建为Activity的适当内部类,并且可以从以下位置正确访问全局“ R”该范围。 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. 我认为这与您的OnClickListener不是您的活动的适当内部类有关。

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"). 首先,创建一个名为“ blahdyblah”的活动,而不只是一个xml文件(因此,您应该有两个活动,您的主要活动(带有带onClickListener的按钮)和一个单独的活动“ 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. 在代码中,将“ MainActivity”替换为您在其中编写此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文件的内容视图设置为R.layout.blahdyblah我敢肯定,这应该可以满足您的需求。

blahdyblah.java blahdyblah.java

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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