简体   繁体   English

Android开发。 RadioButton保持取消选择状态

[英]Android Development. RadioButton keeps unselecting

I am building a little quiz app and let's say on Question 1, I select option B, then submit and the quiz gives me the next question. 我正在构建一个小测验应用,假设在问题1上,我选择选项B,然后提交,测验给了我下一个问题。 However for question 2 if I try to select B, the RadioButton quickly unchecks itself and it is completely uncheckable, until I select another radio button and then try B again. 但是对于问题2,如果我尝试选择B,则RadioButton会自动取消选中它,并且它是完全不可选中的,直到我选择另一个单选按钮,然后再次尝试B。 The pattern is, whatever option I selected in the previous question, is uncheckable in the next question unless I click on a different radiobutton and then try again. 该模式是,无论我在上一个问题中选择的选项如何,在下一个问题中都无法选中该模式,除非我单击其他单选按钮然后重试。 I'm attaching my code. 我正在附上我的代码。 Any help please? 有什么帮助吗?

public class MainActivity extends AppCompatActivity { 公共类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();

}

} }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM