如何禁用 RadioGroup 直到选中复选框 [英] How to disable a RadioGroup until checkbox is checked

查看:16
本文介绍了如何禁用 RadioGroup 直到选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选组,在我的应用程序中选择特定复选框之前,我不希望用户能够选择任何按钮.如果未选中该复选框,则会禁用无线电组.我该怎么做.

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.

推荐答案

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

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);
}

这篇关于如何禁用 RadioGroup 直到选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆