Java 我可以在android中将android:id声明为数组吗?

Java 我可以在android中将android:id声明为数组吗?,java,android,layout,Java,Android,Layout,我有这个密码 rele[0] = new ReleController(findViewById(R.id.button1), 1, this); rele[1] = new ReleController(findViewById(R.id.button2), 2, this); rele[2] = new ReleController(findViewById(R.id.button3), 3, this); rele[3] = new ReleController(findViewById(

我有这个密码

rele[0] = new ReleController(findViewById(R.id.button1), 1, this);
rele[1] = new ReleController(findViewById(R.id.button2), 2, this);
rele[2] = new ReleController(findViewById(R.id.button3), 3, this);
rele[3] = new ReleController(findViewById(R.id.button4), 4, this);
我的按钮布局如下所示:

<LinearLayout style="@style/CoreLayout">

    <ImageButton
        android:id="@+id/button1"
        style="@style/Button.Blue"
        android:src="@mipmap/ic_arrow_left" />

    <ImageButton
        android:id="@+id/button2"
        style="@style/Button.Green"
        android:src="@mipmap/ic_arrow_right"/>
</LinearLayout>

<LinearLayout style="@style/CoreLayout">

    <ImageButton
        android:id="@+id/button3"
        style="@style/Button.Yellow"
        android:src="@mipmap/ic_arrow_up"/>

    <ImageButton
        android:id="@+id/button4"
        style="@style/Button.Purple"
        android:src="@mipmap/ic_arrow_down"/>
</LinearLayout>

谢谢您的回答。

您可以使用数组存储视图id。只需按如下方式声明数组:

for(int i = 0; i < 4; i++)
{
  rele[i] = new ReleController(findViewById(R.id.button[i+1]), i+1, this);
}
android:id="@+id/button[1]"
android:id="@+id/button[2]"
android:id="@+id/button[3]"
...
int[] buttonId = new int[] { R.id.button1, R.id.button2, R.id.button3, R.id.button4 }

然后遍历它

不。。。。。但是你可以使用
int[]={R.id.1,R.id.2,R.id.N}
也许你的意思是这样的[检查最有价值的答案],如果我想要的功能没有在Android中实现,这是最好的选择。非常感谢。我真丢脸,我自己也没有想到这一点:)。如果说“写这么多行”太烦人的话,那就看看巴特诺夫吧。在查找和绑定视图时,它通常可以节省大量的键入工作。