0

你好:)所以这就是我的“应用程序”应该如何工作。有多个活动,在每个活动中,都有一个问题,可以选择答案radiobuttons。它最终到达显示结果的最终活动。

这是我第一个活动的代码

`public class Quiz extends Activity {
    Button btn;
    RadioGroup rg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz);

        btn = (Button) findViewById(R.id.nextBtn);
        rg = (RadioGroup) findViewById(R.id.rg);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (rg.getCheckedRadioButtonId() == -1) {

                    Toast.makeText(getApplicationContext(), "Please select an answer",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(getApplicationContext(), Quiz1.class);
                    Bundle bundle = new Bundle();
                    int id = rg.getCheckedRadioButtonId();
                    RadioButton radioButton = (RadioButton) findViewById(id);
                    bundle.putString("rg", radioButton.getText().toString());
                    intent.putExtras(bundle);
                    startActivity(intent);

                }

            }

        });
      }
    }

第二个活动:

public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle = getIntent().getExtras();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg1", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

第三:

public class Quiz2 extends Activity {

    Button btn;
    RadioGroup rg2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz2);


        btn = (Button) findViewById(R.id.nextBtn2);
        rg2= (RadioGroup) findViewById(R.id.rg2);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (rg2.getCheckedRadioButtonId() == -1) {

                    Toast.makeText(getApplicationContext(), "Please select an answer",
                            Toast.LENGTH_SHORT).show();
                } else{
                    Intent intent = new Intent(getApplicationContext(), Quiz3.class);
                    Bundle bundle = getIntent().getExtras();
                    int id = rg2.getCheckedRadioButtonId();
                    RadioButton radioButton = (RadioButton) findViewById(id);
                    bundle.putString("rg2", radioButton.getText().toString());
                    intent.putExtras(bundle);
                    startActivity(intent);

                }

            }

        });
      }
    }

这一趋势持续了 7 项活动

这是最终代码:

public class Final1 extends Activity {

Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView.setText(bundle.getCharSequence("rg6"));

    btn = (Button)findViewById(R.id.restartBtn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

  }
}

这是我在运行应用程序后在最后一页中得到的内容,textViews 显示文本设置为在最终结果页面之前选择的选项......其余的 textViews(所以 textView1、2、3、4、5、6 ) 都是空的

感谢您的帮助,谢谢<3

4

2 回答 2

0

请改用Sharedpreferences

制作以下课程并在需要时使用它。

    public class PreferenceManager {
    private static final String PREFERENCES_QUIZ_ANS = "quiz_ans";
    private static final String QUIZ_ANS_1 = "quiz_ans_1";

    private static SharedPreferences getQuizAnsPreferences(Context context) {
        return context.getSharedPreferences(PREFERENCES_QUIZ_ANS, Activity.MODE_PRIVATE);
    }

    public static void storeQuizAns_1(Context context, String quiz_ans_1){
        getServicePreferences(context).edit().putString(QUIZ_ANS_1, quiz_ans_1).commit();
    }

    public static String getQuizAns_1(Context context){
        return getServicePreferences(context).getString(QUIZ_ANS_1, null);
    }
}

在每个活动中使用它来存储答案。

PreferenceManager.storeQuizAns_1(getApplicationContext(), radioButton.getText().toString());

在 FINAL 活动中使用它来获取答案并打印到TEXTVIEW

TextView textView1 = (TextView)findViewById(R.id.txt1); textView1.setText(PreferenceManager.getQuizAns_1(this));

您必须尽可能多地制作像 storeQuizAns_1 和 getQuizAns_1 这样的方法。您必须尽可能多地制作像 QUIZ_ANS_1 这样的常量。

于 2016-01-07T09:34:55.467 回答
0

对于您的情况,保存和检索数据的最佳方法是使用Sharedpreferences.

在您的所有活动中,将 sharedpreferences 初始化为

SharedPreferences prefs = this.getSharedPreferences("myprefs", Context.MODE_PRIVATE);

而不是保存在捆绑包中,另存为

prefs.edit.putString("rg2", radioButton.getText().toString()).apply();

然后,在您的最终活动中检索为

prefs.getString("rg2","");

如果你想使用 Intents 来做,你必须在每个活动中将旧数据传递给新活动。所以,在你的第二堂课上,

intent.putExtras("rg",bundle.getString("rg")); );
intent.putExtras("rg1", radioButton.getText().toString());

等等..

于 2016-01-07T06:36:10.933 回答