我有一个10个问题的琐事游戏,每个问题有10秒计时器。当计时器用完时,问题会变为下一个,但计时器永远不会重新启动。它保持在0.我似乎无法找到代码中的哪个位置,以便在显示下一个问题时保持计时器重新启动。
此外,感谢代码结构建议!
提前谢谢。
QuestionView.java
public class QuestionView extends Activity {
int correctAnswers = 0;
int wrongAnswers = 0;
int answer = 0;
int i = 0;
long score = 0;
long startTime = 10000;
long interval = 1000;
long timeElapsed;
boolean timerHasStarted = false;
String category;
Button answer1, answer2, answer3, answer4;
TextView question, timer, timeElapsedView;
ArrayList<Question> queries;
Timer cdTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
answer3 = (Button)findViewById(R.id.answer3);
answer4 = (Button)findViewById(R.id.answer4);
question = (TextView)findViewById(R.id.question);
category = getIntent().getStringExtra("category");
queries = getIntent().getParcelableArrayListExtra("queries");
timer = (TextView)findViewById(R.id.timer);
timeElapsedView = (TextView)findViewById(R.id.timeElapsedView);
cdTimer = new Timer(startTime, interval);
loadQuestion();
}
public void loadQuestion() {
if(i == 9) {
endQuiz();
} else {
if(!timerHasStarted) {
cdTimer.start();
timerHasStarted = true;
} else {
cdTimer.cancel();
timerHasStarted = false;
}
answer = queries.get(i).getCorrectAnswer();
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(0);
if(answer == 0) {
correctAnswers++;
nextQuestion();
} else {
wrongAnswers++;
nextQuestion();
}
}
});
//Same code for button 1 for buttons 2 through 4.
}
}
public void nextQuestion() {
score = score + ((startTime / 100) - timeElapsed);
i++;
loadQuestion();
}
public class Timer extends CountDownTimer {
public Timer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
if(i == 9) {
cdTimer.cancel();
} else {
wrongAnswers++;
nextQuestion();
}
}
@Override
public void onTick(long millisUntilFinished) {
timer.setText("Time remaining: " + (millisUntilFinished / 100));
timeElapsed = 100 - ((startTime - millisUntilFinished) / 100);
timeElapsedView.setText("Points remaining: " + timeElapsed);
}
}
public void endQuiz() {
Intent intent = new Intent(QuestionView.this, Results.class);
intent.putExtra("correctAnswers", correctAnswers);
intent.putExtra("wrongAnswers", wrongAnswers);
intent.putExtra("score", score);
intent.putParcelableArrayListExtra("queries", queries);
intent.putExtra("category", category);
startActivity(intent);
}
}
答案 0 :(得分:0)
尝试以下操作,看看是否有帮助。此外,在计时器自己的cancel()
方法中调用onFinish()
是不必要的,因为计时器已被取消以达到该方法。
public void loadQuestion() {
if(i == 9) {
endQuiz();
} else {
cdTimer.start(); //start timer here
answer = queries.get(i).getCorrectAnswer();
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
cdTimer.cancel(); // end it when clicked.
queries.get(i).setSelectedAnswer(0);
if(answer == 0) {
correctAnswers++;
nextQuestion();
} else {
wrongAnswers++;
nextQuestion();
}
}
});
//Same code for button 1 for buttons 2 through 4.
}
}