【问题标题】:Android Development. RadioButton keeps unselecting安卓开发。 RadioButton 不断取消选择
【发布时间】:2018-02-12 04:52:59
【问题描述】:

我正在构建一个小测验应用程序,假设在问题 1 中,我选择选项 B,然后提交,测验给了我下一个问题。但是对于问题 2,如果我尝试选择 B,RadioButton 会快速取消选中自身并且完全无法选中,直到我选择另一个单选按钮然后再次尝试 B。模式是,无论我在上一个问题中选择了什么选项,在下一个问题中都无法选中,除非我单击不同的单选按钮然后重试。我附上我的代码。请问有什么帮助吗?

公共类 MainActivity 扩展 AppCompatActivity {

QuestionBank allQuestions = new QuestionBank();
String pickedAnswer = "", correctAnswer = "";
final int numberOfQuestions = allQuestions.list.size();
int questionNumber = 0;
boolean noSelection = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nextQuestion();
}

private void nextQuestion() {
    if (questionNumber <= numberOfQuestions - 1) {
        TextView questionLabel = (TextView) findViewById(R.id.question_text_view);
        String fullQuestion = allQuestions.list.get(questionNumber).questionSet.get("question").toString();
        fullQuestion += "\n\na) " + allQuestions.list.get(questionNumber).questionSet.get("a");
        fullQuestion += "\nb) " + allQuestions.list.get(questionNumber).questionSet.get("b");
        fullQuestion += "\nc) " + allQuestions.list.get(questionNumber).questionSet.get("c");
        fullQuestion += "\nd) " + allQuestions.list.get(questionNumber).questionSet.get("d");
        correctAnswer = allQuestions.list.get(questionNumber).questionSet.get("answer").toString();

        questionLabel.setText(fullQuestion);
        questionNumber++;
    } else {
        restart();
    }
}

public void getSelectedAnswer() {
    RadioButton radio_1 = (RadioButton) findViewById(R.id.option1_button);
    RadioButton radio_2 = (RadioButton) findViewById(R.id.option2_button);
    RadioButton radio_3 = (RadioButton) findViewById(R.id.option3_button);
    RadioButton radio_4 = (RadioButton) findViewById(R.id.option4_button);


    if (radio_1.isChecked()) {
        pickedAnswer = "a";
        radio_1.setChecked(false);
    } else if (radio_2.isChecked()) {
        pickedAnswer = "b";
        radio_2.setChecked(false);
    } else if (radio_3.isChecked()) {
        pickedAnswer = "c";
        radio_3.setChecked(false);
    } else if (radio_4.isChecked()) {
        pickedAnswer = "d";
        radio_4.setChecked(false);
    } else {
        noSelection = true;
    }


}

public void submitAnswer(View view) {
    getSelectedAnswer();
    if (noSelection) {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
        a_builder.setMessage("Please select an answer!");
        a_builder.show();
        noSelection = false;
    } else {
        checkAnswer();
        nextQuestion();
    }
}

public void checkAnswer() {

    if (correctAnswer == pickedAnswer) {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
        a_builder.setMessage("Right Answer!");
        a_builder.show();
    } else {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
        a_builder.setMessage("Wrong Answer!");
        a_builder.show();
    }
    pickedAnswer = "";
    correctAnswer = "";

}


public void restart() {
    questionNumber = 0;
    //Collections.shuffle(allQuestions.list);
    nextQuestion();

}

}

【问题讨论】:

  • if (correctAnswer == pickedAnswer)。这不是比较字符串的方法。更改为if (correctAnswer.equals(pickedAnswer))
  • 何时以及如何调用submitAnswer()
  • 也当没有选择答案时correctAnswer.equals(pickedAnswer)。不过可能对您的问题无关紧要
  • 谢谢,我将比较更改为您建议的。当他们点击下一个问题的按钮时调用 submitAnswer()。

标签: java android mobile


【解决方案1】:

在提交后或显示下一个问题之前,在所有按钮上调用setChecked(false)

【讨论】:

  • 我按照你的建议在不同的地方试过这个,按钮没有被选中,但是当我尝试点击同一个选项时,问题仍然存在。
猜你喜欢
  • 2011-07-07
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多