我正在做一个quiz
,因为我有 3 个活动,,,,每个Question1
都有4 个可能的答案(按钮),只有一个按钮是正确的。我必须在测验结束时计算并显示用户的分数。Question2
Question3
我做了一些研究,SHAREDPREFERENECES
似乎是解决方案。但我不知道如何使用它可以请任何人帮忙。Question1.java
代码如下:假设这btnAnswer1a
是正确答案,我如何保存数据,然后textview
在最后一个活动中显示它。
Button Answer1, Answer2, Answer3, Answer4;
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences settings;
SharedPreferences.Editor editor;
int ScoreCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.answer1);
settings = getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
ScoreCount = settings.getInt("ScoreCount", 10);
Answer1 = (Button) findViewById(R.id.btnAnswer1a);
Answer2 = (Button) findViewById(R.id.btnAnswer1b);
Answer3 = (Button) findViewById(R.id.btnAnswer1c);
Answer4 = (Button) findViewById(R.id.btnAnswer1d);
Answer1.setOnClickListener(this);
Answer2.setOnClickListener(this);
Answer3.setOnClickListener(this);
Answer4.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.btnAnswer1a:
Intent Screen1 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen1);
editor.putInt("ScoreCount", 6);
editor.commit();
break;
case R.id.btnAnswer1b:
Intent Screen = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen);
break;
case R.id.btnAnswer1c:
Intent Screen3 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen3);
break;
case R.id.btnAnswer1d:
Intent Screen2 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen2);
break;
}