gpt4 book ai didi

android - 如何初始化 View (ToggleButtons)数组?

转载 作者:行者123 更新时间:2023-11-30 03:06:09 24 4
gpt4 key购买 nike

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

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

最佳答案

试试这段代码:

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

}

有任何问题反馈给我

关于android - 如何初始化 View (ToggleButtons)数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com