[問題] Layout不能切換
各位dev好,最近我在嘗試:按下不同的button,會切換Layout
那目前遇到的problem是,
假設我按了button1 --> 成功切換至 Layout_1
接著我按了button2 --> 並不會從 Layout_1 切換至 Layout_2
畫面就一直停在 Layout_1
======= 以下為code的主要部份:
public class MainActivity extends Activity {
int[] imgId = {R.drawable.building};
private Button btn_1,btn_2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("首頁");
btn_1 = (Button)findViewById(R.id.button1);
btn_2 = (Button)findViewById(R.id.button2);
btn_1.setOnClickListener(myLister); //將這兩個button共用同一個Listener
btn_2.setOnClickListener(myLister);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//以下是我困惑的地方,不知道是否何處有寫錯?
public Button.OnClickListener myLister = new Button.OnClickListener(){
public void onClick(View v){
switch (v.getId()){
case R.id.button1:{
setContentView(R.layout.layout1);
setTitle("Layout1");
break;
}
case R.id.button2:{
setContentView(R.layout.layout2);
setTitle("Layout2");
break;
}
}
}
};
}
===== End
想請問這寫法哪裡有誤? 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.115.110.189
※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1401429818.A.235.html
推
05/30 15:32, , 1F
05/30 15:32, 1F
→
05/30 15:32, , 2F
05/30 15:32, 2F
→
05/30 16:34, , 3F
05/30 16:34, 3F
→
05/30 17:07, , 4F
05/30 17:07, 4F
→
05/30 17:09, , 5F
05/30 17:09, 5F
推
05/31 00:49, , 6F
05/31 00:49, 6F