切换布局白色onBackPressed()

时间:2016-05-14 10:17:16

标签: java android layout

我想使用物理后退按钮在布局之间切换。我发现了这个:

@Override
public void onBackPressed() {
RelativeLayout layout2 = (RelativeLayout)findViewById(R.id.layout2);// check if layout2 is open
if(layout2.getVisibility() == View.VISIBLE){
    setContentView(R.layout.main);
    return;
}else{
    super.onBackPressed(); 
    }

}

检查部分是否正确?

1 个答案:

答案 0 :(得分:0)

全局RelativeLayout十进制,以便您可以使用OnBack()方法

进行访问
 RelativeLayout layout2

这一行你必须放入OnCreate()方法

 layout2 = (RelativeLayout)findViewById(R.id.layout2);// 

最后检查visibility

layout
@Override
public void onBackPressed() {
if(layout2.getVisibility() == View.VISIBLE){
    setContentView(R.layout.main);
    return;
}else{
    super.onBackPressed(); 
    }

}

享受编码............