欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android自定义数字时钟代码,android自定义时钟,package jp.t

来源: javaer 分享于  点击 39020 次 点评:197

Android自定义数字时钟代码,android自定义时钟,package jp.t


package jp.tsmsogn.digitalclock;import java.util.Calendar;import android.content.Context;import android.os.Handler;import android.os.SystemClock;import android.text.format.DateFormat;import android.util.AttributeSet;import android.util.Log;import android.widget.TextView;public class DigitalClock extends TextView {    private final static String TAG = "DigitalClock";    private Calendar mCalendar;    private String mFormat = "yyyy.M.d E";    private Runnable mTicker;    private Handler mHandler;    private boolean mTickerStopped = false;    public DigitalClock(Context context) {        super(context);        initClock(context);    }    public DigitalClock(Context context, AttributeSet attrs) {        super(context, attrs);        initClock(context);    }    private void initClock(Context context) {        if (mCalendar == null) {            mCalendar = Calendar.getInstance();        }    }    @Override    protected void onAttachedToWindow() {        mTickerStopped = false;        super.onAttachedToWindow();        mHandler = new Handler();        mTicker = new Runnable() {            public void run() {                if (mTickerStopped)                    return;                mCalendar.setTimeInMillis(System.currentTimeMillis());                // setText(mSimpleDateFormat.format(mCalendar.getTime()));                setText(DateFormat.format(mFormat, mCalendar));                invalidate();                long now = SystemClock.uptimeMillis();                // long next = now + (1000 - now % 1000);                long next = now + (1000 - System.currentTimeMillis() % 1000);                // Debug                Log.d(TAG, "" + now);                Log.d(TAG, "" + next);                Log.d(TAG, "" + mCalendar.getTimeInMillis());                // TODO                mHandler.postAtTime(mTicker, next);            }        };        mTicker.run();    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        mTickerStopped = true;    }    public void setFormat(String format) {        mFormat = format;    }}
相关栏目:

用户点评