1

我在一个活动中有 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);

我确定应该有更好的方法(比如循环),知道吗?你会怎么做?

4

1 回答 1

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 ++ ;

    }

在任何问题上反馈我

于 2014-02-15T14:22:43.103 回答