我有我不希望用户能够选择任何按钮,直到一个特定的复选框被选中在我的应用程序单选按钮组。如果该复选框取消选中是那么这将禁用无线电集团。我该如何去这样做。
I have a radio group which I do not want to user to be able to select any of the buttons until a particular checkbox is selected within my app. If the checkbox is unticked then this disables the radio-group. How do I go about doing this.
真正的技巧是遍历所有的孩子查看(在这种情况下:复选框
),并调用它的的setEnabled(布尔)
The real trick is to loop through all children view (in this case: CheckBox
) and call it's setEnabled(boolean)
像这样的东西应该做的伎俩:
Something like this should do the trick:
//initialize the controls
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1);
//set setOnCheckedChangeListener()
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
//basically, since we will set enabled state to whatever state the checkbox is
//therefore, we will only have to setEnabled(checked)
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(checked);
}
}
});
//set default to false
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}