如何根据android studio中保存在共享首选项中的单选按钮id启用单选组的特定单选按钮
我正在开发测验应用程序。其中上一个问题和下一个问题有两个按钮。我想做的是当用户从选项中选择答案(选项有4个单选按钮)并尝试下一个问题时,但如果用户想查看上一个问题,则应启用用户以前选择的同一个单选按钮。为此,我使用共享首选项来存储选定的单选按钮id。 这是我的密码: 有关“下一步”按钮,请单击:如何根据android studio中保存在共享首选项中的单选按钮id启用单选组的特定单选按钮,android,radio-button,sharedpreferences,radio-group,Android,Radio Button,Sharedpreferences,Radio Group,我正在开发测验应用程序。其中上一个问题和下一个问题有两个按钮。我想做的是当用户从选项中选择答案(选项有4个单选按钮)并尝试下一个问题时,但如果用户想查看上一个问题,则应启用用户以前选择的同一个单选按钮。为此,我使用共享首选项来存储选定的单选按钮id。 这是我的密码: 有关“下一步”按钮,请单击: nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onC
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);
}
}
previousButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
当前的问题--;
如果(currentQuizQuestion将标签添加到每个单选按钮选项optionOne.setTag(“回答标签”,“一”)。
存储所选标签,并将radiobuttons.getTag(“应答标签”)与存储的标签进行比较