Android取消选择单选按钮,在组中,如何取消选中单选按钮?_android_开发99编程知识库...

問題就在你的心裡 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);

}

});

希望這有幫助。請不要忘記把這個標記為答案,如果它解決了問題。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值