我想知道你是否可以帮助我。我想在java for android中简化这段代码。有办法吗?
btn1.setText(rnd[0]);
btn2.setText(rnd[1]);
btn3.setText(rnd[2]);
btn4.setText(rnd[3]);
btn5.setText(rnd[4]);
我正在考虑for循环。
for(int i=0;i<5; i++) {
btn1.setText(rnd[i]);
}
但是如何更改btn的数量呢?可能吗? 谢谢。
答案 0 :(得分:2)
final List<Button> myButtons = Arrays.asList(btn1, btn2, btn3, btn4, btn5)
for(int i = 0; i < 5; i++) {
myButtons.get(i).setText(rnd[i])
}
答案 1 :(得分:2)
Button[] btn_arr=new Button[rnd.length];
for(int i = 0; i < rnd.length; i++) {
btn_arr[i].setText(rnd[i]);
}