3

LinearLayout にプログラムでいくつかのチェックボックスを追加しました。LinearLayout 内のすべての CheckBoxes を見つけてチェックすることは可能ですか? 私のxml構造は次のとおりです。

<LinearLayout>
<ScrollView>
<TableLayout>
<TableRow>
<CheckBox>
</TableRow>
<TableRow>
<CheckBox>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>

Java コード:

CheckBox selectAll = (CheckBox)findViewById(R.id.checkboxSelectAll);
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(buttonView.isChecked()){
        //loop in linear layout
            //find each checkbox
            //setChecked(true);

    }

}
});
4

2 に答える 2

3

チェックボックスの参照を保持するか、getChildCount() を使用してすべての要素をループし、次に getChildAt を使用して、要素が (instanceof) CheckBox のタイプであるかどうか、および CheckBox にキャストされているかどうかを確認し、それをチェック済みに設定することができます。真実。

LinearLayout yourLayout= (LinearLayout) view.findViewById(R.id.linearLayout);
int count = yourLayout.getChildCount();
for (int i = 0; i < count; i++) {
    View v = rootLinearLayout.getChildAt(i);
    if (v instanceof CheckBox) {
        ((CheckBox) v).setChecked(true);
    }
}

それが役立つことを願っています

于 2017-08-28T11:30:42.750 に答える
1

プログラムで追加された場合CheckBoxes、それらへの参照をActivityクラスのメンバー変数に保持できます。

于 2012-08-17T07:26:46.050 に答える