如何在Android的动态设置布局布局、动态、如何在、Android

2023-09-05 04:26:59 作者:十年好唱却难等

那么,假设有一个名为MainActivity的活动,并有两种布局称为布局1和布局2都有几个按钮。默认情况下MainActivity布局布局1像以下内容:

Well, Suppose there is an ACTIVITY called "MainActivity" and there are two layouts called "layout1" and "layout2" both have few buttons. By default "MainActivity" layout is "layout1" like following:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout1);

现在我所做的其实就是通过点击第二个布局设置喜欢按照布局1的按钮:

Now what I did actually is by clicking a BUTTON in "layout1" the second layout is set like following:

    someBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.layout2);
        }
    });

有在布局2另一个按钮,返回到布局1类似如下:

There are another button in layout2 to return back to layout1 like following:

    someBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.layout1);
        }
    });

问题是当我回布局1,然后someBtn1的OnClickListener是行不通的。看来我需要设置OnClickListener再次someBtn1,布局1的。 我怎么能写code,使他们完美的工作与最佳实践???

Problem is when i returned back to "layout1" then OnClickListener of "someBtn1" is not working. It seems I need to set OnClickListener again for "someBtn1" of "layout1". How I can write code to make them working perfectly with best practices ???

推荐答案

如果你只是想玩弄你目前的code,你的问题的解决方案是,听众必须重新声明的布局发生变化时,如如下:

If you just want to play around with your current code, a solution for your problem is that the listeners must be redeclared when the layout changes, as follows:

someBtn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.layout2);

        someBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.layout1);
            }
        });
    }
});

someBtn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.layout1);

        someBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.layout2);
            }
        });
    }
});

另一种方法,以避免声明听众两次是声明两种方法来处理布局的变化,并使用该按钮的的onClick 属性在每个布局,例如

An alternative to avoid declaring the listeners twice is to declare two methods to handle the layout changes and use the onClick property of the button in each of the layouts, for example:

public void setLayout1(View view) {
    setContentView(R.layout.layout1);
}

public void setLayout2(View view) {
    setContentView(R.layout.layout2);
}

layout1.xml

<Button
    android:id="@+id/someBtn1"
    android:onClick="setLayout2"/>

layout2.xml

<Button
    android:id="@+id/someBtn2"
    android:onClick="setLayout1"/>

不过,如果你想遵循最佳做法,最好的做法是不要混合布局在同一个活动,而是声明了两个不同的活动(每个人有自己的布局),并调用一个活动或其他根据按钮被点击。假设你是在活动1,想叫活性2,然后再回到活动1:

However, if you want to follow best practices, the best practice is not to mix layouts in the same activity, but instead declare two different activities (each one with its own layout) and call one activity or the other depending on the button that was clicked. Suppose that you are in Activity1 and want to call Activity2, then go back to Activity1:

Activity1.java

someBtn1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         startActivity(new Intent(this, Activity2.class));
     }
 });

Activity2.java

someBtn2.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         finish();
     }
 });