在for循环中声明变量,将整数转换为字符串(Android/Java)

在for循环中声明变量,将整数转换为字符串(Android/Java),java,android,Java,Android,可能重复: 我在活动中有这种类型的代码: ImageButton button1 = (ImageButton)findViewById(R.id.butid1); ImageButton button2 = (ImageButton)findViewById(R.id.butid2); ImageButton button3 = (ImageButton)findViewById(R.id.butid3); 现在我想在for循环中声明这些变量。此外,将使用这些N按钮执行操作。例如: for

可能重复:

我在活动中有这种类型的代码:

ImageButton button1 = (ImageButton)findViewById(R.id.butid1);
ImageButton button2 = (ImageButton)findViewById(R.id.butid2);
ImageButton button3 = (ImageButton)findViewById(R.id.butid3);
现在我想在for循环中声明这些变量。此外,将使用这些N按钮执行操作。例如:

for (int NUMBER = 1; NUMBER <= N; NUMBER++) {
    ImageButton button"NUMBER" = (ImageButton)findViewById(R.id.butid"NUMBER");
    button"NUMBER".setEnabled(false);
}

for(int NUMBER=1;NUMBER在循环之前,将所有按钮放入一个按钮数组中。因此,您可以随意在该数组中迭代

ImageButton[] buttons = new ImageButton[<put here the total number of Buttons>];

buttons[0] = (ImageButton)findViewById(R.id.butid1);
buttons[1] = (ImageButton)findViewById(R.id.butid2);
...
buttons[N-1] = (ImageButton)findViewById(R.id.butidN);

for (int i = 0; i < N; i++) {
    buttons[i].setEnabled(false);
}
ImageButton[]按钮=新建ImageButton[];
按钮[0]=(图像按钮)findViewById(R.id.1);
按钮[1]=(图像按钮)findViewById(R.id.d2);
...
按钮[N-1]=(图像按钮)findViewById(R.id.butidN);
对于(int i=0;i
使用
ImageButton[]button=newimagebutton[NUMBER]
然后您的for循环应该是:

for (int i = 0; i < NUMBER; i++) {
    ImageButton button[i] = (ImageButton)findViewById(R.id [***]);
    button[i].setEnabled(false);
}
for(int i=0;i

[*]我不知道安卓系统,但我想有一种方法可以将所有按钮作为某种列表或数组,这样你就不需要通过Id访问每个按钮。

为什么不使用按钮数组?或者arraylist如果N是可变的,我为安卓系统做了一个小的启动程序,在这方面做得不多。但是你不能使用像ImageButton这样的数组[]button=new ImageButton[3];然后使用button[i]=(ImageButton)findViewById(R.id.butid+Integer.toString(i));Regardsame问题:没有必要否决这个问题,因为用户显然是stackoverflow和Android开发的新手。@MártonMolnár如果标题与我的不一样,我就找不到它。别管我的编辑了。它都有效……我很糟糕:P