0

我正在做一个测验应用程序我有一个带有 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));
                        }      
         });
4

2 回答 2

1

问题在于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<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);
            }
        });

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

于 2013-02-13T08:10:07.607 回答
0

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

clearCheck() 

例如你的

radiogroupname.clearCheck();
于 2013-02-13T07:26:30.153 回答