Re: [問題] 多個Button的onClick事件
感謝幾位推文的朋友,以下是我採用的方式
請先在 main.xml上 每個button都加入 android:onClick="myClickHandler"
接著回到程式上
先建立一個Button陣列
private Button[] mButton = new Button[10];
再把每個按鈕都丟到陣列裡頭
mbutton[0] = (Button)findViewById(R.id.button1);
mbutton[1] = (Button)findViewById(R.id.button2);
一直到10
最後收尾
public void myClickHandler(View v) {
for (int i = 1 ; i <21 ; ++i){
if(mButton[i] == v)
mButton[i].setText("你好");
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.124.77.157
推
02/13 17:14, , 1F
02/13 17:14, 1F
→
02/13 17:14, , 2F
02/13 17:14, 2F
推
02/13 17:22, , 3F
02/13 17:22, 3F
→
02/13 17:22, , 4F
02/13 17:22, 4F
→
02/13 17:30, , 5F
02/13 17:30, 5F
→
02/13 17:32, , 6F
02/13 17:32, 6F
→
02/13 17:57, , 7F
02/13 17:57, 7F
可以用耶!! 謝謝
※ 編輯: jakevin 來自: 140.124.77.157 (02/13 19:20)
推
02/13 19:26, , 8F
02/13 19:26, 8F
→
02/13 20:13, , 9F
02/13 20:13, 9F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 5 篇):
AndroidDev 近期熱門文章
PTT數位生活區 即時熱門文章