我可以将计时器设置为从10秒开始倒数,但是下一页时间将从0开始。如何在每次运行时重置时间?
这是我的代码。
private TextView TIMER;
private int Timerr = 10;
public void Show_Next_Quiz(){
Random random = new Random();
int Random_Num = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(Random_Num);
Question1.setText(quiz.get(6)+quizCount);
Right_Answer = quiz.get(0);
quiz.remove(6);
Collections.shuffle(quiz);
iV1.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV2.setImageResource(
getResources().getIdentifier(quiz.get(1), "drawable", getPackageName())
);
iV3.setImageResource(
getResources().getIdentifier(quiz.get(2), "drawable", getPackageName())
);
iV4.setImageResource(
getResources().getIdentifier(quiz.get(3), "drawable", getPackageName())
);
iV5.setImageResource(
getResources().getIdentifier(quiz.get(4), "drawable", getPackageName())
);
iV6.setImageResource(
getResources().getIdentifier(quiz.get(5), "drawable", getPackageName())
);
ans1.setText(quiz.get(0).substring(0,(quiz.get(0).length()-1)));
ans2.setText(quiz.get(1).substring(0,(quiz.get(1).length()-1)));
ans3.setText(quiz.get(2).substring(0,(quiz.get(2).length()-1)));
ans4.setText(quiz.get(3).substring(0,(quiz.get(3).length()-1)));
ans5.setText(quiz.get(4).substring(0,(quiz.get(4).length()-1)));
ans6.setText(quiz.get(5).substring(0,(quiz.get(5).length()-1)));
Count_Down();
quizArray.remove(Random_Num);
}
public void Count_Down(){
new CountDownTimer(12*1000,1*1000){
@Override
public void onTick(long millisUntilFinished) {
TIMER.setText(String.valueOf(Timerr));
Timerr--;
}
@Override
public void onFinish() {
TIMER.setVisibility(View.INVISIBLE);
TIMER.setText(String.valueOf(Timerr));
}
}.start();
在函数CountDown
中,我同时将TIMER.setText(String.valueOf(Timerr))
(onTick和onFinish)都放在了其中,但是它不能工作。我如何使计时器为10秒。每次?
答案 0 :(得分:0)
为CountDownTimer
设置全局变量。
CountDownTimer countDownTimer;
private void timer(){
countDownTimer = new CountDownTimer(12*1000,1*1000){
@Override
public void onTick(long millisUntilFinished) {
TIMER.setText(String.valueOf(Timerr));
Timerr--;
}
@Override
public void onFinish() {
TIMER.setVisibility(View.INVISIBLE);
TIMER.setText(String.valueOf(Timerr));
}
}.start();
}
取消计时器
countDownTimer.cancel();
对于完成计时器
countDownTimer.onFinish();
对于重启/启动计时器
countDownTimer.start();