java - 如何在Java中选中不同的单选按钮时显示不同的消息?

标签 java android if-statement for-loop changelistener

我想在选中第一个单选按钮时显示“您单击了错误的答案”,在选中第二个单选按钮时显示“您单击了正确的答案”。使用此代码,我收到错误:无法解析符号“CorrectAnswer”。 bariable 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,这意味着第二个答案是正确的。
谢谢!

最佳答案

i get an error: Cannot resolve symbol 'CorrectAnswer'

因为 CorrectAnswer 变量不在尝试访问它的方法范围内。

要么将其声明为全局变量,要么使用RadioButtonsetTag/getTag来获取onCheckedChanged中的CorrectAnswer

我认为使用 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...
  }

关于java - 如何在Java中选中不同的单选按钮时显示不同的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33531962/

相关文章:

c++ - 如何在 if 语句中将 unsigned int 用作普通 int

java - 如何使用maven war插件过滤applicationContext

java - 我们可以在没有 main() 方法的情况下执行程序吗?

android - Kotlin 。接口(interface)作为参数

java - if 语句错误

c++ - If 语句导致程序因递归而崩溃

xml - XSLT - 标签不匹配

java - 如何在 Apache PDFBox 中呈现彩色文本

JavaFX LineChart 不画圆?

java - 使用 EditText 更新 SeekBar