如何在选中复选框之前禁用RadioGroup

W0l*_*lf7 30 checkbox android radio-group

我有一个广播组,我不想让用户能够选择任何按钮,直到我的应用程序中选择了一个特定的复选框.如果未选中该复选框,则会禁用无线电组.我该怎么做呢

ari*_*ayu 63

真正的诀窍是遍历所有子视图(在这种情况下:) CheckBox并调用它setEnabled(boolean)

像这样的东西应该做的伎俩:

//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);
}
Run Code Online (Sandbox Code Playgroud)


小智 8

RadioGroup 不能直接禁用,我们必须循环遍历单选按钮并将Enabled 设置为 false。

// To disable the Radio Buttons of radio group.
    for (int i = 0; i < radioGroup.getChildCount(); i++) {
        radioUser.getChildAt(i).setEnabled(false);
    }
Run Code Online (Sandbox Code Playgroud)


Kis*_*iya 8

如果您只有几个单选按钮,更好的方法是为所有孩子设置可点击(false)

radiobutton1.setClickable(false);
radiobutton2.setClickable(false);
radiobutton3.setClickable(false);
Run Code Online (Sandbox Code Playgroud)