我可以把它放到for循环中吗?

Dec*_*nna 1 java eclipse android for-loop r.java-file

可能重复:
android:如何优雅地设置许多按钮ID

这是一个用eclipse制作的android程序.我尝试在imageButton1的位置使用字符串连接无效.R是生成的类,所以我不能进入它并编辑它,以便imageButtons是数组的一部分.如何将其置于for循环中?

    seatButton[0] = (ImageButton) findViewById(R.id.imageButton1);
    seatButton[1] = (ImageButton) findViewById(R.id.imageButton2);
    seatButton[2] = (ImageButton) findViewById(R.id.imageButton3);
    seatButton[3] = (ImageButton) findViewById(R.id.imageButton4);
    seatButton[4] = (ImageButton) findViewById(R.id.imageButton5);
    seatButton[5] = (ImageButton) findViewById(R.id.imageButton6);
    seatButton[6] = (ImageButton) findViewById(R.id.imageButton7);
    seatButton[7] = (ImageButton) findViewById(R.id.imageButton8);
    seatButton[8] = (ImageButton) findViewById(R.id.imageButton9);
    seatButton[9] = (ImageButton) findViewById(R.id.imageButton10);
Run Code Online (Sandbox Code Playgroud)

Joh*_*erg 5

你可以,一种方法如下:

ImageButton[] btns = {R.id.imageButton1, R.id.imageButton2, ..., R.id.imageButton10};
for(int i = 0, len = btns.length; i < len; i++) {
    seatButton[i] = (ImageButton) findByViewId(btns[i]);
}
Run Code Online (Sandbox Code Playgroud)