How to uncheck or clear the radiobutton group?

元气小坏坏 提交于 2019-12-23 02:05:25

问题


I'm new to android..

Im facing a problem in uncheck the radio button in a group....

My problem is....

I am developing an quiz based app. there will be 1 quest and 4 opt(radio buttons) when user opens this app radio button will be unchecked but the prob 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 quest.How can i do it?

Another problem which im facing is... If suppose the user selected first button and again he want to change the option so he select another button that time he should not able to select..

How to implement this?

Any help would be appreciated.

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
     ques1=new ArrayList<String>(new ArrayList<String>(ques1));
        //  j=0;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j));
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) findViewById(checkedId);   // error       
                                                                          //  is here 205 line..
                String temp = radioButton.getText().toString();
                crrtans=new ArrayList<String>(new ArrayList<String>(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{
                //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                   // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
                    txtRadio.setTextColor(Color.parseColor("#FF0000"));
            }
            }
     });
    Button nextBtn = (Button) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){
    //    btn_practicerg.clearCheck();
          if (j == ques1.size() -1) {
                //finish();
                showAlert1();
            }
          else{ 
            ++j;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText(ques1.get(j));
            ++k;
            btn_practice1.setText(answ1.get((k*4)+0));
            btn_practice2.setText(answ1.get((k*4)+1));
            btn_practice3.setText(answ1.get((k*4)+2));
            btn_practice4.setText(answ1.get((k*4)+3));
          }
          btn_practicerg.clearCheck();
          btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    String temp = radioButton.getText().toString();
                    l++;
                    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{
                    //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                       // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                        TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                        txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                        txtRadio.setTextColor(Color.parseColor("#FF0000"));
                }
                }      
         });
     }
});   
    Button previousbtn = (Button) findViewById(R.id.prv_btn);
    previousbtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
        if (j <= 0) {
            Toast.makeText(Question.this, "First Question",Toast.LENGTH_SHORT).show();
            } else {
            --j;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText(ques1.get(j));
            --k;
            btn_practice1.setText(answ1.get((k*4)+0));
            btn_practice2.setText(answ1.get((k*4)+1));
            btn_practice3.setText(answ1.get((k*4)+2));
            btn_practice4.setText(answ1.get((k*4)+3));
            }
         btn_practicerg.clearCheck();
         btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    String temp = radioButton.getText().toString();
                    l--;
                    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{
                    //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                       // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                        TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                        txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                        txtRadio.setTextColor(Color.parseColor("#FF0000"));
                }
                }      
         });
         }
    });
    }

Logcat

            E/AndroidRuntime(893): FATAL EXCEPTION: main
            E/AndroidRuntime(893): java.lang.ArrayIndexOutOfBoundsException
            E/AndroidRuntime(893):  at java.util.ArrayList.get(ArrayList.java:313)
            E/AndroidRuntime(893):  at 
         com.example.finalpractice.Question$LoadQuestions$2$1.onCheckedChanged(Question.java:243)
            E/AndroidRuntime(893):  at 
         android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
            E/AndroidRuntime(893):  at android.widget.RadioGroup.access$600(RadioGroup.java:52)
            E/AndroidRuntime(893):  at 
         android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
            E/AndroidRuntime(893):  at 
         android.widget.CompoundButton.setChecked(CompoundButton.java:127)
            E/AndroidRuntime(893):  at 
         android.widget.CompoundButton.toggle(CompoundButton.java:86)
            E/AndroidRuntime(893):  at android.widget.RadioButton.toggle(RadioButton.java:72)
            E/AndroidRuntime(893):  at 
         android.widget.CompoundButton.performClick(CompoundButton.java:98)
            E/AndroidRuntime(893):  at android.view.View$PerformClick.run(View.java:9080)
            E/AndroidRuntime(893):  at android.os.Handler.handleCallback(Handler.java:587)
            E/AndroidRuntime(893):  at android.os.Handler.dispatchMessage(Handler.java:92)

回答1:


In my case I have a radiogroup named rb and 2 radiobuttons (rbY , rbN) inside of it.


So, this is my implementation using the onClickListener:

final RadioGroup rb = (RadioGroup) convertView.findViewById(R.id.creditcards);
                RadioButton rbY = (RadioButton) convertView.findViewById(R.id.radioButton);
                RadioButton rbN = (RadioButton) convertView.findViewById(R.id.radioButton2);

                final View finalConvertView = convertView;

                rbY.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int selected = rb.getCheckedRadioButtonId();
                        RadioButton rbYes = (RadioButton) finalConvertView.findViewById(selected);
                        RadioButton rbN = (RadioButton) finalConvertView.findViewById(R.id.radioButton2);

                        if (rbYes.isSelected()) {
                            rbYes.setSelected(false);
                            rb.clearCheck();
                            rb.clearChildFocus(rbYes);
                        } else {
                            rbYes.setSelected(true);
                        }
                    }
                });

                rbN.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int selected = rb.getCheckedRadioButtonId();
                        RadioButton rbNo = (RadioButton) finalConvertView.findViewById(selected);

                        if (rbNo.isSelected()) {
                            rbNo.setSelected(false);
                            rb.clearCheck();
                            rb.clearChildFocus(rbNo);
                        }
                        else {
                            rbNo.setSelected(true);
                        }
                    }
                });



回答2:


I found this to be useful https://github.com/ragunathjawahar/deselectable-radio-button. It's a custom radio button that will allow for you to de-select an option thats already been checked.




回答3:


To check/uncheck radio button you can use radioButton.setChecked(true); (or false to uncheck)




回答4:


Change your code like find id's for all radio buttons in onCreate() and make it as global...

in the onClickListener of next button write radioButton.setChecked(false); for all your radio buttons what you used...and remove the line btn_practicerg.clearCheck();

i tried like this working fine..




回答5:


Create clearRadioButtons method and call in onCreate.

public void clearRadioButtons() {
    radiogroup.clearCheck();
}


来源:https://stackoverflow.com/questions/14685873/how-to-uncheck-or-clear-the-radiobutton-group

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