问题描述
我在多次重复倒数计时器时遇到问题,在我的例子中是 12 次.我制作了两个倒数计时器,一个用于循环时间,一个用于暂停,效果很好.问题是暂停结束时我希望循环时间倒计时自动重新开始,直到 12 轮暂停结束.我使用for循环.有没有更好的方法?
I am having a problem repeating countdown timer multiple times,in my case 12 times. I have made two countdown timers,one for roundtime and one for pause and works great.Problem is when the pause is finihed I want that roundtime countdown starts again automaticly until 12 rounds with pause are finished.I use for loop.Is there any better way?
这里是代码:当用户点击按钮倒计时开始
Here is code: when user clicks the button countdown starts
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
long roundtime= Long.parseLong(String.valueOf(msg1))*1000;//user has picked roundtime
Counting timer = new Counting(roundtime, 100);//class Counting extends CountdownTimer
for(int i=0;i<12;i++){
timer.start(); // It goes just ones!
}
}
和倒计时:
class Counting extends CountDownTimer {
public Counting(long roundtime, long countDownInterval) {
super(roundtime, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText("" + String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
@Override
public void onFinish() { // When roundtime is finished,start pause time!
int seconds = msg1 % 60;
int minutes = (msg1 / 60) % 60;
textView.setText(String.format("%02d : %02d", minutes, seconds));
long pause= Long.parseLong(String.valueOf(msg2))*1000; //user has picked pause time
Counting2 timer2 = new Counting2(pause, 100);
timer2.start();
}
}
class Counting2 extends CountDownTimer {
public Counting2(long pause, long countDownInterval) {
super(pause, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
textView2.setText("" + String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
@Override
public void onFinish() {
int seconds = msg2 % 60;
int minutes = (msg2 / 60) % 60;
textView2.setText(String.format("%02d : %02d", minutes, seconds));
}
}
});
}
推荐答案
尝试忙等待让前一个计时器完成,然后再次启动它,它会重复 12 次.>
Try busy waiting for the previous timer to finish and then start it again, and it'll do so 12 times.
for(int i=0;i<12;i++){
while(!previousTimerActive);
timer.start();
previousTimerActive = true;
}
并在您的 Counting2
的 onFinish()
中设置指标 previousTimerActive = false;
.现在,您只需要看看您希望以何种方式将此 boolean previousTimerActive
设为全局"(以便可以从您的活动和您的类 Counting2
访问它).您可以获取它并在 SharedPreferences 中设置它或使用第三方工具(如 EventBus)发送它的值从一个到另一个.
And in your Counting2
's onFinish()
set the indicator previousTimerActive = false;
. Now you just need to see what way you want this boolean previousTimerActive
to be 'global' (so it can be accessed from both your activity and your class Counting2
). You can get it and set it in the SharedPreferences or use a third-party tool like EventBus to send it's value from one to the other.
这篇关于如何多次循环倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!