【问题标题】:How to display different messages when different radio buttons are checked in Java?在Java中选中不同的单选按钮时如何显示不同的消息?
【发布时间】:2015-11-04 21:03:29
【问题描述】:

我想在选中第一个单选按钮时显示“您单击了错误的答案”,在选中第二个单选按钮时显示“您单击了正确的答案”。使用此代码,我得到一个错误:Cannot resolve symbol '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,这意味着第二个答案是正确的。
谢谢!

【问题讨论】:

    标签: java android if-statement for-loop changelistener


    【解决方案1】:

    我收到一个错误:无法解析符号“CorrectAnswer”

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

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

    我认为使用 setTag/getTag 方法将CorrectAnswer 设置为:

    1.首先使用RadioButton保存值使用setTag

    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...
      }
    

    【讨论】:

    • 当我将CorrectAnswer 声明为全局变量时,我收到此错误:Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference 我如何使用RadioButton setTag/getTag 来获得RadioButton 中的onCheckedChanged 值?可以举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 2014-04-04
    • 1970-01-01
    • 2018-04-17
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多