【问题标题】:How to uncheck or clear the radiobutton group?如何取消选中或清除单选按钮组?
【发布时间】:2020-08-03 04:07:27
【问题描述】:

我是安卓新手..

我在取消选中组中的单选按钮时遇到问题......

我的问题是……

我正在开发一个基于测验的应用程序。当用户打开此应用程序时,将有 1 个任务和 4 个选择(单选按钮)单选按钮将被取消选中,但是当用户回答 1 个任务并且当他进行下一个任务单选按钮时,将出现问题。我想取消选中/重置每个任务的单选按钮。我该怎么做?

我面临的另一个问题是......如果假设用户选择了第一个按钮,然后他想再次更改选项,所以他选择了另一个按钮,那时他应该无法选择......

如何实现?

任何帮助将不胜感激。

    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)

【问题讨论】:

  • 任何人回答我帮助我..
  • 代码中的 Question.java:205 行在哪里?
  • 见上文我在命令行中提到过...帮助我
  • 你试试RadioButton radioButton = (RadioButton)group. findViewById(checkedId);而不是RadioButton radioButton = (RadioButton) findViewById(checkedId);
  • 是的,我试过了..我不明白..我选择的选项在下一个问题中也选择了相同的选项..我无法取消选中...我做错了什么请帮助我..

标签: android radio-button


【解决方案1】:

在我的例子中,我有一个名为 rb 的无线电组和其中的 2 个单选按钮(rbYrbN)。


所以,这是我使用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】:

    我发现这很有用https://github.com/ragunathjawahar/deselectable-radio-button。这是一个自定义单选按钮,可让您取消选择已选中的选项。

    【讨论】:

      【解决方案3】:

      创建clearRadioButtons方法并调用onCreate。

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

      【讨论】:

        【解决方案4】:

        要选中/取消选中单选按钮,您可以使用radioButton.setChecked(true);(或 false 取消选中)

        【讨论】:

        • RadioGroup 类型的方法 setChecked(boolean) 未定义,它显示如下错误...
        • 是 RadioButton 的方法,不是 RadioGroup。
        【解决方案5】:

        更改您的代码,例如为 onCreate() 中的所有单选按钮查找 id 并将其设为全局...

        在next按钮的onClickListener中写radioButton.setChecked(false);对于您使用的所有单选按钮...并删除行 btn_practicerg.clearCheck();

        我试过这样工作正常..

        【讨论】:

        • arrayindexoutofboundexception 是因为你正在获得超出该索引的价值
        • 我说删除行 btn_practicerg.clearCheck();你这样做了吗
        猜你喜欢
        • 2014-05-11
        • 2013-06-18
        • 2013-06-06
        • 2021-06-26
        • 2013-04-03
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多