2013-02-13 137 views
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)); 
         }  
     }); 
+0

檢查我的答案.... – 2013-02-13 08:10:30

回答

0

要清除所有單選按鈕,也就是沒有一個組中的單選按鈕的要被檢查使用

clearCheck() 

例如烏爾

radiogroupname.clearCheck(); 
+0

它顯示錯誤.. E/AndroidRuntime(712):\t在com.example.finalpractice.Question $ LoadQuestions $ 1.onCheckedChanged(Question.java:236) E/AndroidRuntime(712 ):\t在android.widget.RadioGroup.setCheckedId(RadioGroup.java:172) E/AndroidRuntime(712):\t在android.widget.RadioGroup.check(RadioGroup.java:166) E/AndroidRuntime(712): \t at android.widget.RadioGroup.clearCheck(RadioGroup.java:205) – 2013-02-13 07:46:43

1

問題在您的public void onCheckedChanged(RadioGroup group, int checkedId) 裏面,您正在創建一個已選中的收音機實例n基於checkedId,一旦你致電clearCheck,它可以爲null。實際情況是,當您單擊下一個按鈕時,您在收音機組上撥打clearCheck,將觸發onCheckedChanged事件並收到空checkedId。因此,您得到一個空單選按鈕,當你這樣做:RadioButton radioButton = (RadioButton)group. findViewById(checkedId);

EDIT(更好的解釋):

加入此方法類:

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); 
      } 
     }); 

希望這對我有所幫助。如果它解決了您的問題,請不要忘記標記爲答案。

+0

什麼?真的,我不能理解你的代碼...請以另一種方式引導我..我想取消選中單選按鈕,而用戶單擊下一步按鈕... – 2013-02-13 08:26:34

+0

看看你的代碼,'onCheckedChanged'監聽器裏面你正在創建一個RadioButton已經通過'checkedId'檢查過了所以如果你的checkedId爲null,RadioButton爲空,所以後面的代碼會產生NullException。所以在做所有這些檢查checkedId是否不爲null。爲了更好的可讀性,我已經將所有代碼封裝在onCheckedChange中並添加了此驗證。 – 2013-02-13 08:31:50

+0

好吧,讓我爲你添加整個代碼。 – 2013-02-13 08:32:38

相關問題