我在下面的代码中多次重复了这个小脚本。我知道我可以运行一个函数来轻松完成此操作,但我可以像在 PHP 中那样使用变量作为变量名吗?
if (4val != null && 4val.length() > 0){
Button 4 = new Button(this);
4.setText(4val);
4.setTextSize(20);
}
我希望能够做类似的事情
i=1;
while{i > 10}{
$$i = value;
//do stuff with $$i
i++;
}
这在 Java 中可行吗?
最佳答案
没有。但是您可以将按钮放在一个数组中,然后循环访问。
Button[] buttons = new Button[10];
// instantiate all the buttons
for (int i = 0; i < buttons.length; i++) {
// update the button
}
关于Java变量名中可以使用变量吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954152/