可我怎么LOOP几个按钮看法? [英] How can I LOOP over several Button Views?
本文介绍了可我怎么LOOP几个按钮看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比方说我在XML文件中定义几个按钮,最初它们设置为禁用。
Say for example I define several buttons in my XML file and initially set them as disabled.
<Button
android:text="Off"
android:id="@+id/Button0"
android:enabled="false">
</Button>
<Button android:text="Off"
android:id="@+id/Button1"
android:enabled="false">
</Button>
<Button android:text="Off"
android:id="@+id/Button2"
android:enabled="false">
</Button>
<Button android:text="Off"
android:id="@+id/Button3"
android:enabled="false">
</Button>
在我的code后来我再要启用所有的按钮所以理论上讲,这应该在一个循环中执行,但我不能工作了我怎么能访问按钮INS循环。例如我不想把几个语句:
Later on in my code I then want to enable ALL the buttons so ideally this should be done in a loop but I cannot work out how I can get access to the buttons ins a loop. e.g. I do not want to put several statements:
Button b = (Button)findViewById(R.id.Button0);
/* Do some action on button0 */
b = (Button)findViewById(R.id.Button1);
/* Do some action on button1 */
/* And continue with explicit statements for each button*/
那么,如何可以实现上述的一个循环?
So how can I achieve the above in a loop?
推荐答案
我的建议是:创建一个包含按钮的标识整数的静态最后一个数组,例如:
My advice: Create a static final array of integers containing the IDs of the buttons, e.g.:
private static final int[] BUTTONS = {
R.id.Button0,
R.id.Button1,
...
};
然后你就可以在该迭代。
Then you can iterate over that.
这篇关于可我怎么LOOP几个按钮看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文