2013-02-24 61 views
1

我正在開發一個測驗,其中有50個隨機顯示的問題和3個選項(單選按鈕),我使用json數組存儲問題。我的問題是如何在用戶單擊單選按鈕(其中一個選項)告訴用戶他/她錯誤或正確之後顯示Toast消息。請幫助我如何實現這一目標。單擊單選按鈕時如何吐司消息

非常感謝。幫助真的很感激!

這是我的代碼:

private void showQuestion(int qIndex, boolean review) { 
    try { 
     JSONObject aQues = Question1.getQuesList().getJSONObject(
       qIndex); 
     String quesValue = aQues.getString("Question"); 
     if (correctAns[qIndex] == -1) { 
      String correctAnsStr = aQues.getString("CorrectAnswer"); 
      correctAns[qIndex] = Integer.parseInt(correctAnsStr); 
     } 

     question.setText(quesValue.toCharArray(), 0, quesValue.length()); 
     answers.check(-1); 
     answer1.setTextColor(Color.BLACK); 
     answer2.setTextColor(Color.BLACK); 
     answer3.setTextColor(Color.BLACK); 
     JSONArray ansList = aQues.getJSONArray("Answers"); 
     String aAns = ansList.getJSONObject(0).getString("Answer"); 
     answer1.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(1).getString("Answer"); 
     answer2.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(2).getString("Answer"); 
     answer3.setText(aAns.toCharArray(), 0, aAns.length()); 
     Log.d("", selected[qIndex] + ""); 
     if (selected[qIndex] == 0) 
      answers.check(R.id.option1); 
     if (selected[qIndex] == 1) 
      answers.check(R.id.option2); 
     if (selected[qIndex] == 2) 
      answers.check(R.id.option3); 

     setText(); 
     if (quesIndex == (Question1.getQuesList().length() - 1)) 
      next.setEnabled(false); 

     if (quesIndex < (Question1.getQuesList().length() - 1)) 
      next.setEnabled(true); 

     if (review) { 
      Log.d("review", selected[qIndex] + "" + correctAns[qIndex]); 

      if (selected[qIndex] != correctAns[qIndex]) { 
       if (selected[qIndex] == 0) 
        answer1.setTextColor(Color.RED); 
       if (selected[qIndex] == 1) 
        answer2.setTextColor(Color.RED); 
       if (selected[qIndex] == 2) 
        answer3.setTextColor(Color.RED); 
      } 
      if (correctAns[qIndex] == 0) 
       answer1.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 1) 
       answer2.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 2) 
       answer3.setTextColor(Color.GREEN); 
     } 
    } catch (Exception e) { 
     Log.e(this.getClass().toString(), e.getMessage(), e.getCause()); 
    } 
} 


private void setAnswer() { 
    if (answer1.isChecked()) 
     selected[quesIndex] = 0; 
    if (answer2.isChecked()) 
     selected[quesIndex] = 1; 
    if (answer3.isChecked()) 
     selected[quesIndex] = 2; 



    Log.d("", Arrays.toString(selected)); 
    Log.d("", Arrays.toString(correctAns)); 

回答

3
RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);   
    mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      // check if the answer is right or wrong and Toast accordingly 
     RadioButton radioButton = (RadioButton) findViewById(checkedId); 
      //check the answer value corresponding to the checkedId RadioButton 
     Toast.makeText(mContext, "...." , Toast.LENGTH_LONG).show(); 
     } 
    });