声明变量在for循环中,转换整数转换为字符串(的Andr​​oid / Java的) [英] Declaring variables in a for loop, converting integers to strings (Android/Java)

查看:304
本文介绍了声明变量在for循环中,转换整数转换为字符串(的Andr​​oid / Java的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  的Andr​​oid findViewbyId具有变异字符串

我有这种类型的code的活动中:

I have this type of code within an Activity:

ImageButton button1 = (ImageButton)findViewById(R.id.butid1);
ImageButton button2 = (ImageButton)findViewById(R.id.butid2);
ImageButton button3 = (ImageButton)findViewById(R.id.butid3);

现在我想一个for循环内声明这些变量。此外,行动将采取与这N的按钮。例如:

Now I want to declare these variables within a for-loop. Also, actions will be taken with these N buttons. For example:

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

N值将取决于各种情况发生变化。我能做些什么最好的解决呢?
(附注:找不到结果在问题,相似的标题)

N will change depending on various situations. What can I do best to solve this? (P.S. Couldn't find the result in the "Questions with similar titles")

推荐答案

循环之前,把所有的按钮你到一个按钮阵列。所以,只要你喜欢,你可以简单地遍历数组。

Before the loop, put all you Buttons into a Button array. So you could simply iterate over the array as you like.

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);
}

这篇关于声明变量在for循环中,转换整数转换为字符串(的Andr​​oid / Java的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆