我正在开发测验应用程序。其中有两个按钮用于上一个问题和下一个问题。我想要做的是当用户从选项中选择答案时(对于有4个单选按钮的选项)并尝试下一个问题但是如果用户想要查看上一个问题那个时间应启用之前用户选择的相同单选按钮。为此,我使用共享首选项来存储选定的单选按钮ID。 这是我的代码: 对于下一个按钮,请单击:
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int radioSelected = radioGroup.getCheckedRadioButtonId();
int userSelection = getSelectedAnswer(radioSelected);
sharedPreferences=getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("Ans",userSelection);
editor.commit();
int correctAnswerForQuestion =firstQuestion.getCorrectAnswer();
if(userSelection == correctAnswerForQuestion)
{
Toast.makeText(getApplicationContext(),"Correct Answer",Toast.LENGTH_LONG).show();
currentQuizQuestion++;
if(currentQuizQuestion>=quizCount)
{
Toast.makeText(getApplicationContext(),"End of quiz",Toast.LENGTH_LONG).show();
return;
}
else
{
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
uncheckedRadioButton();
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
}
else
{
Toast.makeText(getApplicationContext(),"You choose wrong answer",Toast.LENGTH_LONG).show();
currentQuizQuestion++;
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
uncheckedRadioButton();
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
}
});
以前的按钮:
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentQuizQuestion--;
if(currentQuizQuestion<0)
{
return;
}
checkedRadioButton();
firstQuestion=parsedObject.get(currentQuizQuestion);
txtquestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers=firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
});
JSON_DATA_WEB_CALL();
}
private void checkedRadioButton() {
int ans=1;
int ans1=sharedPreferences.getInt("Ans",ans);
if(optionOne.getId()==ans1)
{
optionOne.setChecked(true);
}
}
请帮帮我。非常感谢。
答案 0 :(得分:1)
为每个单选按钮选项选项添加标签.One.setTag(&#34; answer_tag&#34;,&#34; one&#34;)。 存储选定的标签,并将radiobuttons.getTag(&#34; answer_tag)与存储的标签进行比较。