how can i uncheck or reset the radio button?

﹥>﹥吖頭↗ 提交于 2019-12-05 09:15:54

问题


I'm new to android domain..

I am developing an quiz based app. There will be 1 question and 4 option(radio buttons) when user opens this app radio button will be unchecked but the problem comes when the user answers 1 quest and when he goes for next quest radio button will be checked.I want to uncheck/reset the radio buttons for every question.How can i do it?

Thank in advance...

  answ1=new ArrayList<String>(new ArrayList<String>(answ1));
        btn_practice1.setText(answ1.get(0));
        btn_practice2.setText(answ1.get(1));
        btn_practice3.setText(answ1.get(2));
        btn_practice4.setText(answ1.get(3));
        btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {             
                RadioButton radioButton = (RadioButton)group. findViewById(checkedId); //line 262
                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;
                default:
                    btn_practice1.setChecked(false);
                    btn_practice2.setChecked(false);
                    btn_practice3.setChecked(false);
                    btn_practice4.setChecked(false);
                }  
        ImageView nextBtn = (ImageView) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
         btn_practicerg.clearCheck();  //line 355
              }
             });

Logcat

     E/AndroidRuntime(729):at    
      com.example.TEENEINSTIEN.Question$LoadQuestions$2.onCheckedChanged(Question.java:262)
      E/AndroidRuntime(729):at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
      E/AndroidRuntime(729):at android.widget.RadioGroup.check(RadioGroup.java:166)
      E/AndroidRuntime(729):at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
      E/AndroidRuntime(729):at   
      com.example.TEENEINSTIEN.Question$LoadQuestions$3.onClick(Question.java:355)
      E/AndroidRuntime(729):at android.view.View.performClick(View.java:2485)
      E/AndroidRuntime(729):at android.view.View$PerformClick.run(View.java:9080)

I try like this, but i can't able to uncheck..


回答1:


I think the problem is with btn_practicerg object i.e if you are creating new RadioGroup object everytime you display a new question then you have to execute RadioGroup.clearCheck() on new btn_practicerg object instead of old one that I think you are doing currently.

Even better:

    btn_practice1.setText(answ1.get(0));
    btn_practice2.setText(answ1.get(1));
    btn_practice3.setText(answ1.get(2));
    btn_practice4.setText(answ1.get(3));
    btn_practice1.setChecked(false);
    btn_practice2.setChecked(false);
    btn_practice3.setChecked(false);
    btn_practice4.setChecked(false);

to uncheck all the buttons in the beginning. I hope this will solve your problem.




回答2:


Put all your buttons in a RadioGroup then when you need to clear them all use RadioGroup.clearCheck();




回答3:


Try this one, the same problem got solved with this solution:

  1. set onclick method to all 4 radiobuttons say radio1Clicked, radio2Clicked & so on
  2. in respective onclick method add below code:

    public void radio1Clicked(View view)
    {
        // Note that I have unchecked  radiobuttons except the one
        // which is clicked/checked by user
        radio2Button.setChecked(false);
        radio3Button.setChecked(false);
        radio4Button.setChecked(false);
    }
    
  3. simillarly do for remaining radiobuttons. Hope this will solve your problem.

Please feel free to ask any queries regarding the solution.




回答4:


Put all in a group, then use:

buttonGroup1.clearSelection();



回答5:


I had just one radio button and I really didn't want to make a radio button group. So basically I added a radio button then left the text empty, added the checkedTextView next to it (overlapping) then linked the onClick function of the checkedTextView to a toggle function ->

public void toggle(View view)
{ 
    if(checkedTextView.isChecked())
    { 
        checkedTextView.setChecked(false); 
        actualRadioBtn.setChecked(false);
    }
    else
    {
        checkedTextView.setChecked(true);
        actualRadioBtn.setChecked(true);
    }
}

Then in your function where you check if that radio button is selected you can simply use

if(actualRadioBtn.isChecked())....



回答6:


if the Radiobutton belong to a radiogroup you can NEVER unchecked alone that button programaticaly, so you must to do a method to unched all first.

    public void unchecked()
     {
     RadioGroup x=findViewById(R.id.NameOfRadiogroup);
     x.clearCheck();
     }  

and then you must to call the method.



来源:https://stackoverflow.com/questions/15945285/how-can-i-uncheck-or-reset-the-radio-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!