android 倒计时 CountDownTimer

想实现 时分秒 00:00:00 倒计时效果 用了 android 自带的类 “CountDownTimer

/**
 * 注释: 倒计时
 * Created by weitf on 16/5/26.
 * Email:weitengfei0212@gmail.com
 */
public class CountDownHelper {
    private CountDownTimer countDownTimer;
    private OnFinishListener listener;
    private TextView countDownTv;
    private static final int SECONDS = 60;    //秒数
    private static final int MINUTES = 60 * 60;    //小时

    public CountDownHelper(TextView textView, int longTime) {
        this.countDownTv = textView;
        countDownTimer = new CountDownTimer(longTime * 1000, 1000 - 10) {
            @Override
            public void onTick(long millisUntilFinished) {
                int time = (int) ((millisUntilFinished + 15) / 1000);
                countDownTv.setText(formatTime(time));
            }

            @Override
            public void onFinish() {
                countDownTv.setText("00:00:00");
                if (listener != null) {
                    listener.onFinish();
                }
            }
        };
    }

    public void startTimer() {
        countDownTimer.start();
    }

    public void cancelTimer() {
        countDownTimer.cancel();
    }


    public void setOnFinishListener(OnFinishListener listener) {
        this.listener = listener;
    }

    /**
     * 完成回调接口
     */
    public interface OnFinishListener {
        void onFinish();
    }

    static long twice = 0, third = 0;
    static long mtmp = 0, mtmp2 = 0;

    public static String formatTime(int first) {
        String formatTime = "";
        if (first < SECONDS) {
            formatTime = "00:00:" + (first < 10 ? "0" + first : first);
        } else if (first < MINUTES) {
            twice = first % 60;
            mtmp = first / 60;
            if (twice == 0) {
                formatTime = "00:" + (mtmp < 10 ? "0" + mtmp : mtmp) + ":00";//只显示分钟
            } else {
                formatTime = "00:" + (mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (twice < 10 ? "0" + twice : twice);    //显示分钟和秒
            }
        } else {
            twice = first % 3600;    //twice为余数 如果为0则小时为整数
            mtmp = first / 3600;
            if (twice == 0) {
                //只剩下小时
                formatTime = "0" + first / 3600 + ":00:00";
            } else {
                if (twice < SECONDS) {
                    formatTime = (mtmp < 10 ? "0" + mtmp : mtmp) + ":00:" + (twice < 10 ? "0" + twice : twice);    //显示小时和秒
                } else {
                    third = twice % 60;    //third为0则剩下分钟 否则还有秒
                    mtmp2 = twice / 60;
                    if (third == 0) {
                        formatTime = ((mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":00");
                    } else {
                        formatTime = ((mtmp < 10 ? "0" + mtmp : mtmp) + ":" + (mtmp2 < 10 ? "0" + mtmp2 : mtmp2) + ":" + (third < 10 ? "0" + third : third));    //还有秒
                    }
                }
            }

        }
        return formatTime;
    }
}


在Activity 里面 这样使用
CountDownHelper countDownHelper = new CountDownHelper(tvCountDown, 10);
countDownHelper.setOnFinishListener(new CountDownHelper.OnFinishListener() {
    @Override
    public void onFinish() {
        Toast.makeText(AddViewActivity.this, " 倒计时 结束了。。。", Toast.LENGTH_SHORT).show();
    }
});
countDownHelper.startTimer();


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值