例如,我在我的 XML 文件中定义了几个按钮,并最初将它们设置为禁用。
<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>
稍后在我的代码中,我想启用所有按钮,所以理想情况下这应该在循环中完成,但我无法弄清楚如何在循环中访问按钮。例如,我不想多说几句:
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*/
那么我怎样才能在循环中实现上述目标呢?