問題就在你的心裡 public void onCheckedChanged(RadioGroup group, int checkedId) 你正在創建一個基於checkedId的選中單選按鈕的實例,它可以在你調用 clearCheck 之後為空。 事實是,當單擊下一個按鈕時,在單選組上調用 clearCheck 時,onCheckedChanged 事件會被激發並且獲得空 checkedId 。 因此,當你執行這裡操作時,會得到一個空的單選按鈕: RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
( 更好的解釋):
將這裡方法添加到你的類中:private void doProcessingWithRadioButton(int checkedId)
{
RadioButton radioButton = (RadioButton)group. findViewById(checkedId);
String temp = radioButton.getText().toString();
switch(btn_practicerg.getCheckedRadioButtonId()){
case R.id.RB1:
if (btn_practice1.isChecked()){
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB2:
if (btn_practice2.isChecked()){
btn_practice1.setChecked(false);
btn_practice3.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB3:
if (btn_practice3.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice4.setChecked(false);
}
break;
case R.id.RB4:
if (btn_practice4.isChecked()){
btn_practice1.setChecked(false);
btn_practice2.setChecked(false);
btn_practice3.setChecked(false);
}
break;
}
crrtans=new ArrayList(new ArrayList(crrtans));
if (temp.equals(crrtans.get(l))){
TextView txtRadio = (TextView) findViewById(R.id.rdtxt);
txtRadio.setText("" + radioButton.getText() +" IS CORRECT");
txtRadio.setTextColor(Color.parseColor("#008000"));
}else{
TextView txtRadio = (TextView) findViewById(R.id.rdtxt);
txtRadio.setText("" + radioButton.getText() +"is INCORRECT");
txtRadio.setTextColor(Color.parseColor("#FF0000"));
}
}
現在,像這樣更改代碼:btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId!= -1)
{
doProcessingWithRadioButton(checkedId);
}
});
希望這有幫助。請不要忘記把這個標記為答案,如果它解決了問題。