我在一个活动中有 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);
我确定应该有更好的方法(比如循环),知道吗?你会怎么做?