选择后单选按钮自动取消选择

时间:2019-01-07 16:06:06

标签: android

我的MCQ应用程序的单选组中有四个单选按钮。当我选择一个单选按钮并转到下一个问题时,如果我选择与上一个按钮相同的单选按钮,则会出现问题,该单选按钮会自动取消选择,但是在选择任何其他按钮之后,它就会起作用。

private int getSelectedAnswer(int radioSelected){
    int answerSelected = 0;
    if(radioSelected == R.id.radio0){
        answerSelected = 1;
    }
    if(radioSelected == R.id.radio1){
        answerSelected = 2;
    }
    if(radioSelected == R.id.radio2){
        answerSelected = 3;
    }
    if(radioSelected == R.id.radio3){
        answerSelected = 4;
    }
    return answerSelected;
}
private void selectedRadioButton(int ansSelected){
    if(ansSelected == 1){
        optionOne.setChecked(true);
    }
    if(ansSelected == 2){
        optionTwo.setChecked(true);
    }
    if(ansSelected == 3){
        optionThree.setChecked(true);
    }
    if(ansSelected == 4){
        optionFour.setChecked(true);
    }
}
private void uncheckedRadioButton(){
    optionOne.setChecked(false);
    optionTwo.setChecked(false);
    optionThree.setChecked(false);
    optionFour.setChecked(false);
}
private void showQuestions(){
    if(currentQuizQuestion >= quizCount){
        currentQuizQuestion=currentQuizQuestion-1;
        Toast.makeText(ShowSingleQuestionsOnline.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
        return;
    }
    else {
        uncheckedRadioButton();
          quizQuestion.setText(1+ currentQuizQuestion + " : " + MyQuestArrList.get(currentQuizQuestion).get("QuestName"));

        int dd=Integer.parseInt(MyQuestArrList.get(currentQuizQuestion).get("QueType"),10);
            optionOne.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption1"));
            optionTwo.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption2"));
            optionThree.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption3"));
            optionFour.setText(MyQuestArrList.get(currentQuizQuestion).get("QueOption4"));
        }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

此现象是正常的,因为未重置单选选项状态。

看看您的getSelectedAnswer()方法:

private int getSelectedAnswer(int radioSelected){
    int answerSelected = 0;
    if(radioSelected == R.id.radio0){
        answerSelected = 1;
    }
    if(radioSelected == R.id.radio1){
        answerSelected = 2;
    }
    if(radioSelected == R.id.radio2){
        answerSelected = 3;
    }
    if(radioSelected == R.id.radio3){
        answerSelected = 4;
    }
    return answerSelected;
}

当您转到下一个问题时,选定的单选选项状态会保留,并且再次单击它时,其值会更改(取消选择)。

为避免这种情况,您需要重置单选选项,然后再转到下一个问题。