博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 商品倒计时(DigitalClock实现倒计时 )
阅读量:5010 次
发布时间:2019-06-12

本文共 5237 字,大约阅读时间需要 17 分钟。

自定义DigitalClock控件:

package com.veally.timesale;import java.util.Calendar;import android.content.Context;import android.database.ContentObserver;import android.os.Handler;import android.os.SystemClock;import android.provider.Settings;import android.util.AttributeSet;import android.widget.DigitalClock;/** *  Custom digital clock * @author veally@foxmail.com */public class CustomDigitalClock extends DigitalClock {    Calendar mCalendar;    private final static String m12 = "h:mm aa";    private final static String m24 = "k:mm";    private FormatChangeObserver mFormatChangeObserver;    private Runnable mTicker;    private Handler mHandler;    private long endTime;    private ClockListener mClockListener;    private boolean mTickerStopped = false;    @SuppressWarnings("unused")    private String mFormat;    public CustomDigitalClock(Context context) {        super(context);        initClock(context);    }    public CustomDigitalClock(Context context, AttributeSet attrs) {        super(context, attrs);        initClock(context);    }    private void initClock(Context context) {        if (mCalendar == null) {            mCalendar = Calendar.getInstance();        }        mFormatChangeObserver = new FormatChangeObserver();        getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);        setFormat();    }    @Override    protected void onAttachedToWindow() {        mTickerStopped = false;        super.onAttachedToWindow();        mHandler = new Handler();        /**         * requests a tick on the next hard-second boundary         */        mTicker = new Runnable() {            public void run() {                if (mTickerStopped)                    return;                long currentTime = System.currentTimeMillis();                if (currentTime / 1000 == endTime / 1000 - 5 * 60) {                    mClockListener.remainFiveMinutes();                }                long distanceTime = endTime - currentTime;                distanceTime /= 1000;                if (distanceTime == 0) {                    setText("00:00:00");                    onDetachedFromWindow();                    mClockListener.timeEnd();                } else if (distanceTime < 0) {                    setText("00:00:00");                } else {                    setText(dealTime(distanceTime));                }                invalidate();                long now = SystemClock.uptimeMillis();                long next = now + (1000 - now % 1000);                mHandler.postAtTime(mTicker, next);            }        };        mTicker.run();    }    /**     * deal time string     *      * @param time     * @return     */    public static String dealTime(long time) {        StringBuffer returnString = new StringBuffer();        long day = time / (24 * 60 * 60);        long hours = (time % (24 * 60 * 60)) / (60 * 60);        long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60;        long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60;        String dayStr = String.valueOf(day);        String hoursStr = timeStrFormat(String.valueOf(hours));        String minutesStr = timeStrFormat(String.valueOf(minutes));        String secondStr = timeStrFormat(String.valueOf(second));        returnString.append(hoursStr).append(":").append(minutesStr).append(":").append(secondStr);        return returnString.toString();    }    /**     * format time     *      * @param timeStr     * @return     */    private static String timeStrFormat(String timeStr) {        switch (timeStr.length()) {        case 1:            timeStr = "0" + timeStr;            break;        }        return timeStr;    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        mTickerStopped = true;    }    /**     * Clock end time from now on.     *      * @param endTime     */    public void setEndTime(long endTime) {        this.endTime = endTime;    }    /**     * Pulls 12/24 mode from system settings     */    private boolean get24HourMode() {        return android.text.format.DateFormat.is24HourFormat(getContext());    }    private void setFormat() {        if (get24HourMode()) {            mFormat = m24;        } else {            mFormat = m12;        }    }    private class FormatChangeObserver extends ContentObserver {        public FormatChangeObserver() {            super(new Handler());        }        @Override        public void onChange(boolean selfChange) {            setFormat();        }    }    public void setClockListener(ClockListener clockListener) {        this.mClockListener = clockListener;    }    public interface ClockListener{        void timeEnd();        void remainFiveMinutes();    }}

使用:

remainTime.setEndTime(mItems.get(position).getRemainTime());        remainTime.setClockListener(new CustomDigitalClock.ClockListener() { // register the clock's listener            @Override            public void timeEnd() {                // The clock time is ended.            }            @Override            public void remainFiveMinutes() {                // The clock time is remain five minutes.            }        });

代码下载:

转载于:https://www.cnblogs.com/guilin-hu/p/5710020.html

你可能感兴趣的文章
css定位position属性深究
查看>>
android中不同版本兼容包的区别
查看>>
Static 与 new 的问题【待解决】
查看>>
xml
查看>>
在 mvc4 WebApi 中 json 的 跨域访问
查看>>
敏捷开发文章读后感
查看>>
xposed获取context 的方法
查看>>
html5 canvas 图像处理
查看>>
He who hesitates is Lost
查看>>
php中引用&的真正理解-变量引用、函数引用、对象引用
查看>>
关于<form> autocomplete 属性
查看>>
OutOfMemory
查看>>
LeetCode:组合总数III【216】
查看>>
Thinkphp框架回顾(三)之怎么实现平常的sql操作数据库
查看>>
虚函数的效率问题
查看>>
POJ 1860 Currency Exchange(SPFA 判断有无“正”环)
查看>>
广告地址屏蔽
查看>>
收缩SqlServer数据库日记方法
查看>>
每日英语:15 places to find inspiration
查看>>
学习方法--提问
查看>>