[Android1.5]DigitalClock自定义日期输出格式
生活随笔
收集整理的這篇文章主要介紹了
[Android1.5]DigitalClock自定义日期输出格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
關鍵字: DigitalClock date format
Android的DigitalClock并沒有設置輸出格式的屬性或方法,但是可以通過繼承重寫來實現,見正文部分代碼。
?
正文
一、需求
修改時間輸出格式為僅顯示小時和分鐘。
?
二、效果圖
?
三、說明
通過看源碼可知,只需修改以下兩行代碼其他全部復制過來即可:
????private?final?static?String?m12?=?"h:mm:ss?aa";????private?final?static?String?m24?=?"k:mm:ss";
如果想做得更好更通用的話可以把設置日期格式的方法暴露出來,或者為其增加一個xml屬性。
?
四、完整代碼
package?com.test;import?java.util.Calendar;
import?android.content.Context;
import?android.content.res.Resources;
import?android.database.ContentObserver;
import?android.os.Handler;
import?android.os.SystemClock;
import?android.provider.Settings;
import?android.text.format.DateFormat;
import?android.util.AttributeSet;
/**
?*?自定義DigitalClock輸出格式
?*?@author?農民伯伯
?*
?*/
public?class?DigitalClock?extends?android.widget.DigitalClock?{
????Calendar?mCalendar;
????private?final?static?String?m12?=?"h:mm?aa";//h:mm:ss?aa
????private?final?static?String?m24?=?"k:mm";//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?=?context.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();
????????}
????}
}
五、使用方法
<com.test.DigitalClock?android:layout_x="15dp"?android:layout_y="30dp"?????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"
????????????android:textColor="@android:color/white"?android:textSize="55sp"
?????????????android:shadowColor="@android:color/white"?android:shadowRadius="2.0"
????????????>
????????</com.test.DigitalClock>
代碼說明:
關于android:shadowColor可以參見我的中文API文檔翻譯。
?
結束
問題解決。
總結
以上是生活随笔為你收集整理的[Android1.5]DigitalClock自定义日期输出格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 文件系统概述
- 下一篇: 赞助博主