我在一个活动中有11个ToggleButtons,目前,在onCreate中,我声明了一个11个ToggleButtons的数组,我在onCreate()中定义,就像那样:
private static ToggleButton toggleButton[] = new ToggleButton[11];
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton[0] = (ToggleButton) findViewById(R.id.ToggleButton00);
toggleButton[1] = (ToggleButton) findViewById(R.id.ToggleButton01);
toggleButton[2] = (ToggleButton) findViewById(R.id.ToggleButton02);
toggleButton[3] = (ToggleButton) findViewById(R.id.ToggleButton03);
toggleButton[4] = (ToggleButton) findViewById(R.id.ToggleButton04);
toggleButton[5] = (ToggleButton) findViewById(R.id.ToggleButton05);
toggleButton[6] = (ToggleButton) findViewById(R.id.ToggleButton06);
toggleButton[7] = (ToggleButton) findViewById(R.id.ToggleButton07);
toggleButton[8] = (ToggleButton) findViewById(R.id.ToggleButton08);
toggleButton[9] = (ToggleButton) findViewById(R.id.ToggleButton09);
toggleButton[10] = (ToggleButton) findViewById(R.id.ToggleButton10);
我确信应该有更好的方法(比如循环),任何想法?你会怎么做?
答案 0 :(得分:1)
试试这段代码:
ToggleButton toggleButton[] = new ToggleButton[11];
int arrayindex = 0;
int viewcount = 0;
while (arrayindex < 11) {
View v = asyncLayout.getChildAt(viewcount);
if (v instanceof ToggleButton) {
toggleButton[arrayindex] = (ToggleButton) v;
arrayindex ++;
}
viewcount ++ ;
}
在任何问题上反馈我