随笔

随记:android实现一个xx:xx:xx的倒计时

2020-03-31  本文已影响0人  向前的zz

需要实现一个倒计时的一个方法,通过这种方式进行了实现

import android.os.CountDownTimer;

/**
 * @author zhousaito
 * @version 1.0
 * @date 2020/3/31 19:03
 * @Dec 给个时间进行倒计时处理  xx:xx:xx
 */
public class CountDownTimerWrap extends CountDownTimer {

    public CountDownTimerWrap(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        int remainTime = (int) (millisUntilFinished / 1000L);
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = remainTime % 3600;
        if (remainTime > 3600) {
            h = remainTime / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = remainTime / 60;
            if (remainTime % 60 != 0) {
                s = remainTime % 60;
            }
        }
        if (countTimerListener != null) {
            countTimerListener.onTime(String.format("%02d", h), String.format("%02d", d), String.format("%02d", s));
        }
    }

    @Override
    public void onFinish() {
        if (countTimerListener != null) {
            countTimerListener.onTimeFinish();
        }
    }

    private ICountTimerListener countTimerListener;

    public void setCountTimerListener(ICountTimerListener countTimerListener) {
        this.countTimerListener = countTimerListener;
    }

    public interface ICountTimerListener {
        void onTime(String hour, String minute, String second);

        void onTimeFinish();
    }
}
上一篇下一篇

猜你喜欢

热点阅读