我在回合结束时设置暂停时遇到问题。当用户单击按钮时,它开始倒计时计时器,当完成的文本返回到我想要的值时。但我希望在倒计时(计时器)完成时启动另一个 timer2。现在它同时开始计数。每个计时器在屏幕上都有自己的 textView。 这是代码:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
long roundtime= Long.parseLong(String.valueOf(msg1))*1000; //User set Time for Round
long pause= Long.parseLong(String.valueOf(msg2))*1000; //User set Time for Pause
Counting timer = new Counting(roundtime, 1000); // class CountdownTimer
Counting2 timer2 = new Counting2(pause, 1000); // class CountdownTimer
timer.start(); // I want to start with this timer first
timer2.start(); // And when timer is finished start with this in own textView
}
最佳答案
使用CountDownTimer .这正是您所需要的。只需启动一个计时器,然后在该计时器的 onFinish()
方法上启动新计时器。像这样的东西:
CountDownTimer timer1 = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//do whatever you need here, this gets called every 1000 milliseconds (the 2nd parameter
of the constructor, the first is total time in ms
}
public void onFinish() {
CountDownTimer timer2 = new CountDownTimer(5000, 1000) {
//same logic
};
timer2.start();
}
};
timer1.start();
关于java - 在倒数计时器中启动另一个计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022301/