如果我第一次设置要选择的单选按钮,它工作正常。但是,如果我通过调用 .setChecked(false); 取消选择它;然后,以后即使我尝试通过调用 setChecked(true) 来选择它,也不会取消选择前一个。
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);
}
}
}
});