我有一個問題重複倒計時器多次,在我的情況下12次。我已經做了兩個倒計時定時器,一個用於輪迴,一個用於暫停並且工作良好。問題是當暫停完成時,我想讓該倒計時開始自動重新開始,直到有12個循環暫停完成。 我用loop.Is有更好的方法嗎?如何循環倒計時定時器多次
這裏是代碼:當用戶點擊該按鈕倒計時開始
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));
}
}
});
}
你看上了一個Handler嗎?它可能提供一個更簡單的實現。 – mjstam
mjstam,不,我沒有。 –