android 倒计时 CountDownTimer

来源:互联网 发布:淘宝如何做全屏首页 编辑:程序博客网 时间:2024/05/07 23:16
想实现 时分秒 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();

1 0
原创粉丝点击