我是初学者,所以不需要向我提问。我有一个活动,当按下某个按钮时,它会在ontick()方法中增加时间。如果单击该按钮,则时间从以下位置更改:
Time left :5 sec
以
Time left :7 sec
如何使用此类实现CountDown计时器的增量?
类别:
public abstract class MyCountDownTimer {
private CountDownTimer cdt;
private long millisInFuture;
private long countDownInterval;
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
this.millisInFuture = millisInFuture;
this.countDownInterval = countDownInterval;
recreateCounter(millisInFuture, countDownInterval);
}
public abstract void onFinish();
public abstract void onTick(long millisUntilFinished);
public void start(){
cdt.start();
}
private void setMillisInFuture(long millisInFuture){
this.millisInFuture = millisInFuture;
}
public void onIncrement(long millis){
millisInFuture += millis;
recreateCounter(millisInFuture, countDownInterval);
}
private void recreateCounter(long millisInFuture, long countDownInterval){
if(cdt != null){
try {
cdt.cancel();
} catch (Exception e) {
}
}
cdt = new CountDownTimer(millisInFuture, countDownInterval) {
@Override
public void onTick(long millisUntilFinished) {
setMillisInFuture(millisUntilFinished);
MyCountDownTimer.this.onTick(millisUntilFinished);
}
@Override
public void onFinish() {
MyCountDownTimer.this.onFinish();
}
};
}
}
非常感谢帮助!