2013-02-24 70 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(); 
     } 
    });