RadioButton的setChecked(true)无效

2019-06-25  本文已影响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);
}
上一篇下一篇

猜你喜欢

热点阅读