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

查看:290
本文介绍了如何禁用一个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.

推荐答案

真正的技巧是遍历所有的孩子查看(在这种情况下:复选框),并调用它的的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);
}

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

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