簡單的修改Android DigitalClock – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

最近在做項目中,需要用到一個時間顯示的控件,由於android自身DigitalClock隻能顯示時間,不能顯示日期,所以自己就動手對DigitalClock做瞭一個小小的改造

android自帶的效果如下:

 

改造後的效果:

 

 

  1 public class TimeWidget extends TextView {

  2

  3     Calendar mCalendar;

  4     private final static String m12 = "h:mm:ss";

  5     private final static String m24 = "k:mm:ss";

  6     private FormatChangeObserver mFormatChangeObserver;

  7 www.aiwalls.com

  8     private Runnable mTicker;

  9     private Handler mHandler;

 10

 11     private boolean mTickerStopped = false;

 12

 13     String mFormat;

 14

 15     private static String[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

 16

 17     public TimeWidget(Context context, AttributeSet attrs, int defStyle) {

 18         super(context, attrs, defStyle);

 19         initClock(context);

 20     }

 21

 22     public TimeWidget(Context context, AttributeSet attrs) {

 23         super(context, attrs);

 24         initClock(context);

 25     }

 26

 27     public TimeWidget(Context context) {

 28         super(context);

 29         initClock(context);

 30     }

 31

 32     private void initClock(Context context) {

 33         Resources r = context.getResources();

 34

 35         if (mCalendar == null) {

 36             mCalendar = Calendar.getInstance();

 37         }

 38

 39         mFormatChangeObserver = new FormatChangeObserver();

 40         //註冊,監聽系統日期設置數據庫的改變

 41         getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);

 42

 43         setFormat();

 44     }

 45

 46     @Override

 47     protected void onDetachedFromWindow() {

 48         super.onDetachedFromWindow();

 49         mTickerStopped = true;

 50     }

 51

 52     /**

 53      * Pulls 12/24 mode from system settings

 54 */

 55     private boolean get24HourMode() {

 56         return android.text.format.DateFormat.is24HourFormat(getContext());

 57     }

 58

 59     private void setFormat() {

 60         if (get24HourMode()) {

 61             mFormat = m24;

 62         } else {

 63             mFormat = m12;

 64         }

 65     }

 66

 67     // format the string of time

 68     private static String format(int t) {

 69         String s = "" + t;

 70         if (s.length() == 1) {

 71             s = "0" + s;

 72         }

 73         return s;

 74     }

 75

 76     @Override

 77     public boolean onTouchEvent(MotionEvent event) {

 78         Log.i("", "TimeWidget.onTouchEvent");

 79

 80         getContext().startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

 81

 82         return super.onTouchEvent(event);

 83     }

 84

 85     @Override

 86     protected void onAttachedToWindow() {

 87         Log.i("", "TimeWidget.onAttachedToWindow");

 88         mTickerStopped = false;

 89         super.onAttachedToWindow();

 90         mHandler = new Handler();

 91

 92         /**

 93          * requests a tick on the next hard-second boundary

 94 */

 95         mTicker = new Runnable() {

 96             public void run() {

 97                 if (mTickerStopped)

 98                     return;

 99                 mCalendar.setTimeInMillis(System.currentTimeMillis());

100

101                 int myear = (mCalendar.get(Calendar.YEAR));

102                 int mmonth = (mCalendar.get(Calendar.MONTH) + 1);// 月份+1是一年中的第幾個月

103                 int mmonthday = (mCalendar.get(Calendar.DAY_OF_MONTH));// 一月中的日期

104                 final int mweekday = (mCalendar.get(Calendar.DAY_OF_WEEK)) – 1;

105

106                 final String mDate = format(myear) + "-" + format(mmonth) + "-" + format(mmonthday);

107

108                 setText(DateFormat.format(mFormat, mCalendar) + "  " + mDate + " " + weekdays[mweekday]);

109                 invalidate();

110                 long now = SystemClock.uptimeMillis();

111                 long next = now + (1000 – now % 1000);

112                 mHandler.postAtTime(mTicker, next);

113             }

114         };

115         mTicker.run();

116     }

117

118     private class FormatChangeObserver extends ContentObserver {

119         public FormatChangeObserver() {

120             super(new Handler());

121         }

122

123         @Override

124         public void onChange(boolean selfChange) {

125             setFormat();

126         }

127     }

128

129 }

  作者 YunLin

發佈留言