4

您好我正在尝试为 Android 创建一个应用程序,为了开发它,我需要浏览不同的页面和问题。对于这个任务,我定义了一个带有一些单选按钮的单选组。我想要获得的是每个问题都回答了单选按钮,当用户浏览不同的页面时,可以检索该值。我已经尝试过这段代码,其中包括如果有一个选定的单选按钮,则没有创建新的单选按钮(单选按钮检查为假)。但是,使用此代码,总是有一个选定的答案,因此总是选择相同的单选按钮。我会很感激一些帮助。

        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);

但是我不知道如何继续。我正在考虑以下代码,但它给了我错误,并且位置是“页码”:

public void Test(){
        switch(posicion){
            case(0):
                if(answerA0==true){
                    e.putBoolean("A0",radBotA.isChecked());
                }
            }
        }
    }
4

3 回答 3

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);
于 2013-01-29T16:05:12.903 回答
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你的问题编号在哪里

于 2014-03-04T12:41:06.420 回答
0

我刚刚解决了这个问题,现在我可以在每次下一次单击时保存单选按钮的当前状态,并且在每次上一次单击时,我都会返回单选状态,即使用户通过转到上一个问题或任何问题更改了状态的问题。

于 2014-04-02T10:50:56.743 回答