美文网首页
RadioButton的setChecked(true)无效

RadioButton的setChecked(true)无效

作者: 周末不加班 | 来源:发表于2019-06-25 23:43 被阅读0次

    首先清空了RadioRoup中的子控件
    通过循环向RadioRoup添加多个RadioButton
    在特定条件时将某个RadioButton设置为点击状态
    radioButton.setChecked(true);无效

    rg_tag.removeAllViews();//下拉刷新时清空历史数据
    for (int i=0; i<list.size(); i++)
    {
        RadioButton radioButton = new RadioButton(mActivity);
        radioButton.setChecked(false);
        radioButton.setButtonDrawable(null);
        radioButton.setGravity(Gravity.CENTER);
        radioButton.setPadding(0,8,0,8);
        radioButton.setText(name);
        radioButton.setTextSize(14);
        ColorStateList colorStateList=getResources().getColorStateList(R.color.edu_rb_bg);
        radioButton.setTextColor(colorStateList);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(18,0,18,0);
        radioButton.setLayoutParams(layoutParams);
        radioButton.setId(id);
        rg_tag.addView(radioButton);
        if( this.checkedId==id  || this.checkedId==0&&i==0)
        {
            radioButton.setChecked(true);
        }
    }
    

    解决方案

    在radioButton.setChecked(true)前添加调用RadioGoup的clearCheck();

    if( this.checkedId==id  || this.checkedId==0&&i==0)
    {
        rg_tag.clearCheck();
        radioButton.setChecked(true);
    }
    

    相关文章

      网友评论

          本文标题:RadioButton的setChecked(true)无效

          本文链接:https://www.haomeiwen.com/subject/ccjbpqtx.html