簡體   English   中英

在Java中選中不同的單選按鈕時,如何顯示不同的消息?

[英]How to display different messages when different radio buttons are checked in Java?

我想在選中第一個單選按鈕時顯示“您單擊了錯誤的答案”,而在選中了第二個單選按鈕時顯示了“您單擊了正確的答案”。 使用此代碼,我得到一個錯誤: Cannot resolve symbol 'CorrectAnswer' 可糾正的CorrectAnswer是數據庫查詢的結果。 這是我的代碼:

    radioGroup = new RadioGroup[2];
    answer = new RadioButton[2];
    int i = 0;
    for (Question qn : questions) {
        radioGroup[i] = new RadioGroup(this);
        int j = 0;
        for (Answer an : answers) {
            if (qn.getID() == an.getQuestion_id_answer()) {
                String answers_log = " " + an.getAnswer();
                Integer CorrectAnswer = an.getCorrect_answer();
                answer[j] = new RadioButton(this);
                answer[j].setText(answers_log);
                radioGroup[i].addView(answer[j]);
                j++;
            }
        }
        linearLayout.addView(radioGroup[i]);

        radioGroup[i].setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case 0:
                        if (CorrectAnswer == 1) {
                            Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 1:
                        if (CorrectAnswer == 1) {
                            Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        });
        i++;
    }

數據庫結構是這樣的:

         db.addAnswer(new Answer("NY", 3, 0));
         db.addAnswer(new Answer("WA", 3, 1));

如您所見,在第三列中我有1,這意味着第二個答案是正確的。
謝謝!

我收到一個錯誤:無法解析符號“ CorrectAnswer”

因為CorrectAnswer變量不在嘗試訪問它的方法范圍內。

無論是把它聲明為全局變量或使用setTag/getTagRadioButton來獲得CorrectAnswer價值onCheckedChanged

我認為使用setTag / getTag方法將CorrectAnswer設置為:

1.首先使用setTag通過RadioButton保存值:

answer[j] = new RadioButton(this);
  answer[j].setTag(String.valueOf(an.getCorrect_answer()));
  answer[j].setText(answers_log);

2.onCheckedChanged獲取選定的RadioButton值為:

 @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
    int  CorrectAnswer=Integer.parseInt(checkedRadioButton.getTag().toString());
    ....your code here...
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM