android從源碼帶你熟悉DigitalClock 數字時針的應用以及它的使用情境

來源:互聯網
上載者:User

android從源碼帶你熟悉DigitalClock 數字時針的應用以及它的使用情境

在android如果表示數字時針一般用DigitalClock,這個類是google給我們開發人員提供的一個方便的類實現數字時針的功能,現在就寫個demo,為什麼要講這個類呢?因為我下篇要講ListView item倒計時功能,所以先把這個基礎練習一下,以及它的實現方式,建立android項目digitalClockdemo

布局activity_main.xml檔案

 

   

代碼都不用寫 直接運行起來就會有效果

 

這個數字是變化的 這隻是靜態圖,為什麼代碼沒寫會出來這個效果,所以就得看下他的源碼了

 

public class DigitalClock extends TextView {    Calendar mCalendar;    private final static String m12 = h:mm:ss aa;    private final static String m24 = k:mm:ss;    private FormatChangeObserver mFormatChangeObserver;    private Runnable mTicker;    private Handler mHandler;    private boolean mTickerStopped = false;    String mFormat;    public DigitalClock(Context context) {        super(context);        initClock(context);    }    public DigitalClock(Context context, AttributeSet attrs) {        super(context, attrs);        initClock(context);    }    private void initClock(Context context) {        Resources r = mContext.getResources();        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;                    mCalendar.setTimeInMillis(System.currentTimeMillis());                    setText(DateFormat.format(mFormat, mCalendar));                    invalidate();                    long now = SystemClock.uptimeMillis();                    long next = now + (1000 - now % 1000);                    mHandler.postAtTime(mTicker, next);                }            };        mTicker.run();    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        mTickerStopped = true;    }    /**     * 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();        }    }}

這個類的源碼不算多,所以有耐心的全部看完,

 

首Crowdsourced Security Testing道了DigitalClock類是繼承此TextView

從建構函式中開始看initClock()方法,代碼如下

private void initClock(Context context) {
Resources r = mContext.getResources();
if (mCalendar == null) {
mCalendar = Calendar.getInstance();
}www.bkjia.com
mFormatChangeObserver = new FormatChangeObserver();
getContext().getContentResolver().registerContentObserver(
Settings.System.CONTENT_URI, true, mFormatChangeObserver);


setFormat();
}

setFormat()方法預設是顯示24小時的,最主要的是onAttachedToWindow()方法,

onAttachedToWindow()是掛載在表單上的 他在activity的onResume()方法之後會執行,

在onAttachedToWindow()方法中關鍵是這段代碼:

mHandler = new Handler();


/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar));
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();

這是整個類的核心,

mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar));

這二行代碼就是顯示在我們案頭上的時間 DigitalClock在內部已經實現了,所以為什麼我們不用寫代碼就看到數字時針時間了,

invalidate();沒時間變化一次 就會重新繪製UI
long now = SystemClock.uptimeMillis();//從開機到現在的毫秒數(手機睡眠的時間不包括在內);
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);

 

long next = now + (1000 - now % 1000);這行代碼錶示沒看懂?

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.