最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

Android之倒计时CountDownTimer公共方法

XAMPP下载 admin 29浏览 0评论

CountDownTimer公共方法

public final void cancel ()
         取消倒计时(取消后,再次启动会重新开始倒计时)
public abstract void onFinish ()
         倒计时完成时被调用
public abstract void onTick (long millisUntilFinished)
         固定间隔被调用     参数 millisUntilFinished   倒计时剩余时间。
public synchronized final CountDownTimer start ()
         启动倒计时

举例:十分钟倒计时

private MyCountDownTimer timer;private final long TIME = 600 * 1000L;
private final long INTERVAL = 1000L;private long leftoverTime;
//十分钟倒计时class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
leftoverTime = millisUntilFinished / 1000;
if (leftoverTime <= 59) {
binding.tvTimeCount.setText(String.format("00:%02d", leftoverTime));
} else {
binding.tvTimeCount.setText(String.format("%02d:%02d", leftoverTime / 60, leftoverTime % 60));}}
@Overridepublic void onFinish() {
cancelTimer();
}
}
/**
* 开始倒计时
*/private void startTimer() {
if (timer == null) {
timer = new MyCountDownTimer(TIME, INTERVAL);
}
timer.start();
}
/**
* 取消倒计时
*/
private void cancelTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}

 

转载请注明:XAMPP中文组官网 » Android之倒计时CountDownTimer公共方法