我正在开发测验应用程序。我在每个页面中显示问题及其选项(单选按钮),其中包含下一个按钮。即,当单击下一个按钮时,将显示下一个问题及其选项。现在我也保留了上一个按钮。但是我遇到了后退按钮的问题,例如,我在第一个问题中选择了第三个选项并转到第二个问题,然后再次单击上一个按钮并返回到第一个问题。所选值在第一个问题中消失了。如何存储选定的单选按钮值以及如何在按下返回或下一步按钮时再次保持单选按钮选中=true。我正在努力解决这个问题。请在这方面帮助我。
我将选定的单选按钮值存储在一个整数变量中。
Code:
int selectedop1 = 0;
public void nextquestion() {
if (in < parts.length - 1) {
in++;
radiogroup.clearCheck();
optionslayout.removeView(radiogroup);
for (int a = 0; a < next2.length; a++) {
next3 = next2[a].trim().split("getchoce");
stringList5.add(next3[0]);
stringList6.add(next3[1]);
stringList7.add(next3[2]);
stringList8.add(next3[3]);
}
optionslayout = (LinearLayout) findViewById(R.id.chklnlayout);
tv.setText(Html.fromHtml(next4[1].replace("\\n", "<br>")));
tv.setTextColor(Color.parseColor("#000000"));
radiogroup = (RadioGroup) new RadioGroup(Test.this);
optionslayout.addView(radiogroup);
for (int p = 0; p < next2.length; p++) {
RadioButton newRadioButton = new RadioButton(Test.this);
newRadioButton.setText(stringList6.get(p));
newRadioButton.setTextColor(Color.parseColor("#000000"));
newRadioButton.setTextSize(15);
radbtn = Integer.parseInt(stringList5.get(p));
newRadioButton.setId(radbtn);
radiogroup.addView(newRadioButton);
}
radiogroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup,
int checkedId) {
for (int h = 0; h < radioGroup.getChildCount(); h++) {
RadioButton btn = (RadioButton) radioGroup
.getChildAt(h);
if (btn.getId() == checkedId) {
selectedop1 = btn.getId();
return;
}
}
}
});
}
else
{
System.out.println("Test completed");
}
}
Previous click:
public void previouscall(View v) {
in--;
if(in >1)
{
prevquestion();
}
}
public void prevquestion() {
if (in < parts.length - 1) {
radiogroup.clearCheck();
optionslayout.removeView(radiogroup);
next1 = parts[in].trim().split("getquopim");
next4 = next1[0].trim().split("getquessplit");
next2 = next1[1].trim().split("getidoptn");
optionslayout = (LinearLayout) findViewById(R.id.chklnlayout);
tv.setText(Html.fromHtml(next4[1].replace("\\n", "<br>")));
tv.setTextColor(Color.parseColor("#000000"));
radiogroup = (RadioGroup) new RadioGroup(Test.this);
optionslayout.addView(radiogroup);
for (int p = 0; p < next2.length; p++) {
RadioButton newRadioButton = new RadioButton(Test.this);
newRadioButton.setText(stringList6.get(p));
newRadioButton.setTextColor(Color.parseColor("#000000"));
newRadioButton.setTextSize(15);
radbtn = Integer.parseInt(stringList5.get(p));
newRadioButton.setId(radbtn);
radiogroup.addView(newRadioButton);
}
radiogroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup,
int checkedId) {
for (int h = 0; h < radioGroup.getChildCount(); h++) {
RadioButton btn = (RadioButton) radioGroup
.getChildAt(h);
if (btn.getId() == checkedId) {
selectedop1 = btn.getId();
return;
}
}
}
});
}
else
{
System.out.println("test completed");
}
}