作者closer1018 (阿紅)
看板AndroidDev
標題[問題] 如何讓跳轉A B
時間Mon Oct 8 00:19:16 2012
Button b;
TextView t, t2;
int count, total = 20;
int u_count = 3;
int flag = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
t = (TextView) findViewById(R.id.textView1);
while(total>0){
if (flag == 1) {
flag = 0;
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
t.setText("A");
total--;
}
});
} else {
flag = 1;
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
t.setText("B");
total--;
}});}}}}
小弟想要做一個
假如flag =1的時候 按下按鈕會印出A
反之 則印出 B
不知道小弟的CODE有什麼問題...
問過朋友說觀念錯誤>"<
不知道小弟哪裡是錯的...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 113.61.141.108
※ 編輯: closer1018 來自: 113.61.141.108 (10/08 00:24)
1F:→ nobody1:oncreate只做一次 所以flag要寫在clicklisener裡面 10/08 00:36
2F:→ nobody1:total的用意是?? 10/08 00:37
total 的用意是...要讓他執行那個總數...
但現在是測試,所以是用20下去代
另外把flag寫在clicklistener裡面這個我有試過
雖然成功但是並不是小弟想要的結果
小弟比較偏向 去判別flag然後有兩個不同的類別
假使flag=1 則執行a()
反之 就執行B()
但先行條件得印得出來@@
所以小弟才這樣測試
※ 編輯: closer1018 來自: 113.61.141.108 (10/08 01:01)
3F:→ tryagainloop:為什麼不把判斷寫在listener 裡面?? 10/08 10:13
4F:→ tryagainloop:flag 沒其他用途為什麼不用boolean ? 10/08 10:13
5F:→ roro0:我也看不懂為什麼不把 flag 寫在 onclicklistener 裡面... 10/09 08:36
6F:→ roro0:如果把 flag 的判斷式寫在 onResume 呢? 不知道是不是你要的 10/09 08:37