[問題] 如何讓跳轉A B

看板AndroidDev作者 (阿紅)時間13年前 (2012/10/08 00:19), 編輯推噓0(006)
留言6則, 3人參與, 最新討論串1/1
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)

10/08 00:36, , 1F
oncreate只做一次 所以flag要寫在clicklisener裡面
10/08 00:36, 1F

10/08 00:37, , 2F
total的用意是??
10/08 00:37, 2F
total 的用意是...要讓他執行那個總數... 但現在是測試,所以是用20下去代 另外把flag寫在clicklistener裡面這個我有試過 雖然成功但是並不是小弟想要的結果 小弟比較偏向 去判別flag然後有兩個不同的類別 假使flag=1 則執行a() 反之 就執行B() 但先行條件得印得出來@@ 所以小弟才這樣測試 ※ 編輯: closer1018 來自: 113.61.141.108 (10/08 01:01)

10/08 10:13, , 3F
為什麼不把判斷寫在listener 裡面??
10/08 10:13, 3F

10/08 10:13, , 4F
flag 沒其他用途為什麼不用boolean ?
10/08 10:13, 4F

10/09 08:36, , 5F
我也看不懂為什麼不把 flag 寫在 onclicklistener 裡面...
10/09 08:36, 5F

10/09 08:37, , 6F
如果把 flag 的判斷式寫在 onResume 呢? 不知道是不是你要的
10/09 08:37, 6F
文章代碼(AID): #1GSQk7wZ (AndroidDev)