我正在创建一个包含 3 个问题/级别的测验。我的问题是我不知道要实现什么或编写如何计算正确答案的代码。例如,用户单击正确的答案(按钮)它将计数并添加,直到他达到第 3 级。在他完成游戏后,我的 complete.class 将弹出并有一个按钮,如果用户单击 highscore(button),它将自动将文本设置为我的 highscore.class
levelone.class
public class EasyOne extends Activity {
ImageButton a, b, c;
Intent intent ;
CountDownTimer cdt;
TextView timer;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (ImageButton) findViewById(R.id.ib_a);
b = (ImageButton) findViewById(R.id.ib_b);
c = (ImageButton) findViewById(R.id.ib_c);
timer = (TextView) findViewById(R.id.tv_timer);
cdt = new CountDownTimer(5000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timer.setText("TIMES UP!");
intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
};
intent = new Intent(getApplicationContext(),ChoiceTwo.class);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
cdt.cancel();
Intent intent = new Intent(getApplicationContext(),ChoiceTwo.class);
startActivity(intent);
}
});
cdt.start();
}
}
complete.class
公共类完成扩展 Activity {
Button highscore;
String highestScore;
int score;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
highscore = (Button) findViewById(R.id.save);
highscore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
if (score > preferences.getInt("score", 0)) {
preferences.edit().putInt("score", score).commit();
}
Intent showHighScore = new Intent(MainActivity.this, highscore.class);
showHighScore.putExtra("current_score", score);
startActivity(showHighScore);
}
});
}
}
高分.class
public class highscore extends Activity {
Button back;
TextView highScore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int currentScore = getIntent().getIntExtra("current_score", 0);
highScore = (TextView) findViewById(R.id.tv_highscore);
highScore.setText(currentScore + " / " + preferences.getInt("score", 0));
int highestScore = -1;
//Now use this score variable to set anywhere.
Bundle extras = getIntent().getExtras();
if (extras != null) {
highestScore = extras.getInt("score");
}
back = (Button) findViewById(R.id.btn_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
}
});
// TODO Auto-generated method stub
}
}