无法取消选中动态单选组中已选中的单选按钮 [英] not able to unchecked the already checked radio button in dynamic radio group

查看:64
本文介绍了无法取消选中动态单选组中已选中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我第一次设置了一个单选按钮,它就可以正常工作.但是如果我通过调用 .setChecked(false); 取消选择它.然后,即使我尝试通过调用 setChecked(true) 使其选中,也不会取消选择前一个.

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling .setChecked(false); then, later even if I try to make it selected by calling setChecked(true) will not unselect the previous one.

    private void radiotype() {
    count = //changed every time.
    LinearLayout llques = (LinearLayout) mview.findViewById(R.id.llrbgRBD);
    RadioGroup group = new RadioGroup(context);
    group.setOrientation(RadioGroup.VERTICAL);
    final RadioButton[] rb = new RadioButton[count];
    List<String[]> ans = getAnswerList.getAns();

    for (int j = 0; j < count; j++) {

        rb[j] = new RadioButton(context);
        rb[j].setVisibility(View.VISIBLE);
        rb[j].setText("`enter code here`hi");
        String a = rb[j].getText().toString();`enter code here`
        Log.e("getAnswerList===a", "getAnswerList===>a" + a);
        Log.e("getAnswerList", "getAnswerList===>" + ans.get(index)[0]);
        if (a.equalsIgnoreCase(ans.get(index)[0])) {
            rb[j].setChecked(true);
        }
        rb[j].setTextColor(Color.BLACK);
        rb[j].setButtonDrawable(R.drawable.custom_radio_button);
        group.addView(rb[j]);
    }
    llques.removeAllViews();
    llques.addView(group);
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            //int c = count;
            for(int i=0;i<count;i++){
                rb[i].setChecked(false);
            }
            //
            Log.v("id",""+checkedId);
            for (int i = 0; i < count; i++) {
                if (rb[i].getId() == checkedId){
                    rb[i].setChecked(true);
                } 
            }
        }
    });

推荐答案

我知道这已经很老了,但我也遇到了同样的问题.解决方案是通过 addView() 将 RadioButton 添加到 RadioGroup 之后改变它的状态.我想这也是 BAKUS 在他的示例代码中试图表达的意思.

I know this is rather old, but I just had the same problem. The solution is to change the state of RadioButton after adding it to RadioGroup by addView(). I guess this is also what BAKUS tried to say by his sample code.

这篇关于无法取消选中动态单选组中已选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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