首先清空了RadioRoup中的子控件
通過循環(huán)向RadioRoup添加多個RadioButton
在特定條件時將某個RadioButton設(shè)置為點擊狀態(tài)
radioButton.setChecked(true);無效
rg_tag.removeAllViews();//下拉刷新時清空歷史數(shù)據(jù)
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)前添加調(diào)用RadioGoup的clearCheck();
if( this.checkedId==id || this.checkedId==0&&i==0)
{
rg_tag.clearCheck();
radioButton.setChecked(true);
}