最近在做项目中,需要用到一个时间显示的控件,由于android自身DigitalClock只能显示时间,不能显示日期,所以自己就动手对DigitalClock做了一个小小的改造

android自带的效果如下:

改造后的效果:

public class TimeWidget extends TextView {      Calendar mCalendar;     private final static String m12 = "h:mm:ss";     private final static String m24 = "k:mm:ss";     private FormatChangeObserver mFormatChangeObserver;      private Runnable mTicker;     private Handler mHandler;      private boolean mTickerStopped = false;      String mFormat;      private static String[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };      public TimeWidget(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         initClock(context);     }      public TimeWidget(Context context, AttributeSet attrs) {         super(context, attrs);         initClock(context);     }      public TimeWidget(Context context) {         super(context);         initClock(context);     }      private void initClock(Context context) {         Resources r = context.getResources();          if (mCalendar == null) {             mCalendar = Calendar.getInstance();         }          mFormatChangeObserver = new FormatChangeObserver();         //注册,监听系统日期设置数据库的改变         getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);          setFormat();     }      @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;         }     }      // format the string of time     private static String format(int t) {         String s = "" + t;         if (s.length() == 1) {             s = "0" + s;         }         return s;     }      @Override     public boolean onTouchEvent(MotionEvent event) {         Log.i("", "TimeWidget.onTouchEvent");          getContext().startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));          return super.onTouchEvent(event);     }      @Override     protected void onAttachedToWindow() {         Log.i("", "TimeWidget.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());                  int myear = (mCalendar.get(Calendar.YEAR));                 int mmonth = (mCalendar.get(Calendar.MONTH) + 1);// 月份+1是一年中的第几个月                 int mmonthday = (mCalendar.get(Calendar.DAY_OF_MONTH));// 一月中的日期                 final int mweekday = (mCalendar.get(Calendar.DAY_OF_WEEK)) - 1;                  final String mDate = format(myear) + "-" + format(mmonth) + "-" + format(mmonthday);                  setText(DateFormat.format(mFormat, mCalendar) + "  " + mDate + " " + weekdays[mweekday]);                 invalidate();                 long now = SystemClock.uptimeMillis();                 long next = now + (1000 - now % 1000);                 mHandler.postAtTime(mTicker, next);             }         };         mTicker.run();     }      private class FormatChangeObserver extends ContentObserver {         public FormatChangeObserver() {             super(new Handler());         }          @Override         public void onChange(boolean selfChange) {             setFormat();         }     }  }

更多相关文章

  1. Android下PreferenceScreen 加载流程
  2. 【Android】android:padding属性设置对ImageButton无效问题
  3. android通知栏Notification
  4. Android(安卓)为【apk】文件签名,增加修改系统时间等权限
  5. android 显示特殊符号
  6. android开机动画bootanimation
  7. Android(安卓)ScrollView的使用
  8. android顶部栏属性ActionBar Options
  9. android中menu的使用

随机推荐

  1. android之simpleAdaper应用
  2. android webview处理下载内容
  3. 转 :android gmail 添加附件
  4. Android(安卓)Handler学习笔记
  5. android 在程序中开启GPS功能
  6. Activity背景全透明
  7. android之PopUpWindow显示Listview(文件列
  8. Android:短信发送器
  9. android QRCode
  10. Android的版本(Version)和API-level的对应