【问题标题】:How to uncheck the radio buttons in group?如何取消选中组中的单选按钮?
【发布时间】:2013-01-28 16:19:02
【问题描述】:

我正在做一个测验应用程序我有一个带有 4 个单选按钮的单选组,我有一个下一步按钮。

在显示第一个问题并回答它没有问题时检查..当用户给出下一个时,单选组应取消选中。如何实现?

当用户选择该选项时,它应该验证答案是否正确。我已将正确答案存储在数组中。当用户切换选项时,它会在切换按钮中显示错误。如何实现?

   btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, 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<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{
              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){
         if (j == ques1.size() -1) {
                showAlert1();
            }
          else{ 
            ++j;
            ++num;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText("Q" + num +ques1.get(j));
                        }      
         });

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    问题在于您的public void onCheckedChanged(RadioGroup group, int checkedId) 您正在创建一个基于checkedId 的选中单选按钮的实例,一旦您调用clearCheck,它就可以为空。事实是,一旦您在单击下一个按钮时在您的单选组上调用clearCheckonCheckedChanged 事件就会被触发,并且它会得到一个空的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<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{
              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);
                }
            });
    

    希望这会有所帮助。如果它解决了您的问题,请不要忘记将其标记为答案。

    【讨论】:

    • 什么?真的我无法理解您的代码...请以另一种方式指导我..我想在用户单击下一步按钮时取消选中单选按钮...
    • 查看您的代码,在onCheckedChanged 侦听器中,您正在创建一个已由checkedId 检查的 RadioButton 实例因此,如果您的checkedId 为null,则RadioButton 为null,因此后面的代码会引发NullException。因此,在进行所有这些检查之前,检查 checkedId 是否不为空。为了更好的可读性,我已将所有代码封装在 onCheckedChange 中并添加了此验证。
    • 它显示这两个错误.. 运算符 != 未定义参数类型 int,null 和 void 是变量 doProcessingWithRadioButton 的无效类型
    • 现在检查,如果它不起作用,给我整个课程。我会编辑它需要的地方并发回给你。
    【解决方案2】:

    要清除所有单选按钮,即不使用要检查的组中的单选按钮

    clearCheck() 
    

    例如你的

    radiogroupname.clearCheck();
    

    【讨论】:

    • 它显示错误.. E/AndroidRuntime(712): at com.example.finalpractice.Question$LoadQuestions$1.onCheckedChanged(Question.java:236) E/AndroidRuntime(712): at android. widget.RadioGroup.setCheckedId(RadioGroup.java:172) E/AndroidRuntime(712): 在 android.widget.RadioGroup.check(RadioGroup.java:166) E/AndroidRuntime(712): 在 android.widget.RadioGroup.clearCheck( RadioGroup.java:205)
    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 2011-01-08
    • 2011-02-05
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多