您好我正在尝试为Android创建应用,为了开发它,我需要浏览不同的页面和问题。为此,我已经定义了一个带有一些radiobutton的radiogroup。我想要获得的是每个问题都回答了radiobutton,当用户进入不同的页面时,可以检索到该值。我已经尝试过这个代码,如果有一个选定的单选按钮,则没有创建新的radiobutton(radiobuttons检查为false)。但是,使用此代码时,始终会有一个选定的答案,因此始终选择相同的单选按钮。我将不胜感激。
radBotA.setOnCheckedChangeListener(radioCheckChangeListener);
radBotB.setOnCheckedChangeListener(radioCheckChangeListener);
radBotC.setOnCheckedChangeListener(radioCheckChangeListener);
radBotD.setOnCheckedChangeListener(radioCheckChangeListener);
radBotA.setOnClickListener(radioClickListener);
radBotB.setOnClickListener(radioClickListener);
radBotC.setOnClickListener(radioClickListener);
radBotD.setOnClickListener(radioClickListener);
if (radBotA.isChecked()){
Answers[position]="A";
}
else if(radBotB.isChecked()){
Answers[position]="B"; }
else if(radBotB.isChecked()){
Answers[position]="C"; }
else if(radBotC.isChecked()){
Answers[position]="D"; }
else if(radBotD.isChecked()){
Answers[position]="D"; }
else {
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
}
bPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
position = position -1;
questions.Previous();
currentQuestion();
}
});
bNext.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
position = position +1;
questions.Next();
currentQuestion();
}
});
private void currentQuestion() {
if (position==0){
bPrevious.setVisibility(View.GONE);
}else{
bPrevious.setVisibility(View.VISIBLE);
}
if (position==nPreguntas-1){
bNext.setVisibility(View.GONE);
}else{
bNext.setVisibility(View.VISIBLE);
}
questions.currentQuestion(this, category);
enunciado.setImageResource(Enunciado[position]);
pregunta.setText(questions.getPregunta());
final RadioButton radBotA = new RadioButton(this);
final RadioButton radBotB = new RadioButton(this);
final RadioButton radBotC = new RadioButton(this);
final RadioButton radBotD = new RadioButton(this);
radBotA.setText("A. " + questions.getRespuestaA());
radBotB.setText("B. " + questions.getRespuestaB());
radBotC.setText("C. " + questions.getRespuestaC());
radBotD.setText("D. " + questions.getRespuestaD());
String nprueba = "Item " + questions.getId() + " de "+ nPreguntas;
NombrePrueba.setText(nprueba);
if (radBotA.isChecked()){
Answers[position]="A";
}
else if(radBotB.isChecked()){
Answers[position]="B"; }
else if(radBotB.isChecked()){
Answers[position]="C"; }
else if(radBotC.isChecked()){
Answers[position]="D"; }
else if(radBotD.isChecked()){
Answers[position]="D"; }
else {
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
}
}
谢谢大家的时间
编辑:
public void save(){
SharedPreferences settings = getSharedPreferences("Answers", 0);
SharedPreferences.Editor e = settings.edit();
e.putBoolean("A0",radBotA.isChecked());
e.putBoolean("B0",radBotB.isChecked());
e.putBoolean("C0",radBotC.isChecked());
e.putBoolean("D0",radBotD.isChecked());
e.putBoolean("A1",radBotA.isChecked());
e.putBoolean("B1",radBotB.isChecked());
e.putBoolean("C1",radBotC.isChecked());
e.putBoolean("D1",radBotD.isChecked());
e.putBoolean("A2",radBotA.isChecked());
e.putBoolean("B2",radBotB.isChecked());
e.putBoolean("C2",radBotC.isChecked());
e.putBoolean("D2",radBotD.isChecked());
e.putBoolean("A3",radBotA.isChecked());
e.putBoolean("B3",radBotB.isChecked());
e.putBoolean("C3",radBotC.isChecked());
e.putBoolean("D3",radBotD.isChecked());
public void load(){
SharedPreferences settings = getSharedPreferences("Answers", 0);
boolean answerA0 = settings.getBoolean("A0", false);
boolean answerB0 = settings.getBoolean("B0", false);
boolean answerC0 = settings.getBoolean("C0", false);
boolean answerD0 = settings.getBoolean("D0", false);
boolean answerA1 = settings.getBoolean("A1", false);
boolean answerB1 = settings.getBoolean("B1", false);
boolean answerC1 = settings.getBoolean("C1", false);
boolean answerD1 = settings.getBoolean("D1", false);
boolean answerA2 = settings.getBoolean("A2", false);
boolean answerB2 = settings.getBoolean("B2", false);
boolean answerC2 = settings.getBoolean("C2", false);
boolean answerD2 = settings.getBoolean("D2", false);
boolean answerA3 = settings.getBoolean("A3", false);
boolean answerB3 = settings.getBoolean("B3", false);
boolean answerC3 = settings.getBoolean("C3", false);
boolean answerD3 = settings.getBoolean("D3", false);
但是我不知道如何继续。我想'以下代码,但它给了我错误,其中posicion是“页码”:
public void Test(){
switch(posicion){
case(0):
if(answerA0==true){
e.putBoolean("A0",radBotA.isChecked());
}
}
}
}
答案 0 :(得分:6)
如果我理解正确,您想要在其他活动中检索一些数据。在这种情况下,最简单的方法是使用SharedPreferences
。
用户回答问题后(CheckBox
的检查状态正在更改),您应该将信息存储在SharedPreferences
中,如下所示:
SharedPreferences settings = getSharedPreferences("Answers", 0); // first argument is just a name of your SharedPreferences which you want to use. It's up to you how you will name it, but you have to use the same name later when you want to retrieve data.
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("questionA", radBotA.isChecked()); // first argument is a name of a data that you will later use to retrieve it and the second argument is a value that will be stored
editor.putBoolean("questionB", radBotB.isChecked());
editor.putBoolean("questionC", radBotC.isChecked());
editor.putBoolean("questionD", radBotD.isChecked());
editor.commit(); // Commit the changes
现在您将这些信息存储在内部存储中。在其他活动中,您可以检索此信息:
SharedPreferences settings = getSharedPreferences("Answers", 0);
boolean answerA = settings.getBoolean("questionA", false); // The second argument is a default value, if value with name "questionA" will not be found
boolean answerB = settings.getBoolean("questionB", false);
boolean answerC = settings.getBoolean("questionC", false);
boolean answerD = settings.getBoolean("questionD", false);
答案 1 :(得分:0)
我正在处理相同的应用程序,解决方案是您必须根据您的问题编号存储单选按钮的状态,并且每个问题都有不同的密钥,如下所示:
final RadioButton rdSelection=(RadioButton)findViewById(mradioOptGroup.getCheckedRadioButtonId());
int child_index=mradioOptGroup.indexOfChild(rdSelection);
switch(mid)
{
case 1:
sharedpreferences.putint("",child_index);
break;
case 2:
sharedpreferences.putint("",child_index);
break;
}
并在下一个问题的所有问题上做到这一点
并且在之前的每个问题上,您都必须存储mid+1
其中mid
是您的问题编号
答案 2 :(得分:0)
我刚刚解决了这个问题,现在我能够在每次下一次点击时保存单选按钮的当前状态,并且在每次上一次点击时我都会回到无线电状态,即使在用户通过转到上一次更改状态时也是如此问题或任何问题。