Android的DigitalClock并没有设置输出格式的属性或方法,但是可以通过继承重写来实现,见正文部分代码。
正文
一、需求
修改时间输出格式为仅显示小时和分钟。
二、效果图
三、说明
通过看源码可知,只需修改以下两行代码其他全部复制过来即可:
1 | private final static String m12 = "h:mm:ss aa" ; |
2 | private final static String m24 = "k:mm:ss" ; |
如果想做得更好更通用的话可以把设置日期格式的方法暴露出来,或者为其增加一个xml属性。
四、完整代码
003 | import java.util.Calendar; |
005 | import android.content.Context; |
006 | import android.content.res.Resources; |
007 | import android.database.ContentObserver; |
008 | import android.os.Handler; |
009 | import android.os.SystemClock; |
010 | import android.provider.Settings; |
011 | import android.text.format.DateFormat; |
012 | import android.util.AttributeSet; |
015 | * 自定义DigitalClock输出格式 |
016 | * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a> 农民伯伯 |
019 | public class DigitalClock extends android.widget.DigitalClock { |
022 | private final static String m12 = "h:mm aa" ; |
023 | private final static String m24 = "k:mm" ; |
024 | private FormatChangeObserver mFormatChangeObserver; |
026 | private Runnable mTicker; |
027 | private Handler mHandler; |
029 | private boolean mTickerStopped = false ; |
033 | public DigitalClock(Context context) { |
038 | public DigitalClock(Context context, AttributeSet attrs) { |
039 | super (context, attrs); |
043 | private void initClock(Context context) { |
044 | Resources r = context.getResources(); |
046 | if (mCalendar == null ) { |
047 | mCalendar = Calendar.getInstance(); |
050 | mFormatChangeObserver = new FormatChangeObserver(); |
051 | getContext().getContentResolver().registerContentObserver( |
052 | Settings.System.CONTENT_URI, true , mFormatChangeObserver); |
058 | protected void onAttachedToWindow() { |
059 | mTickerStopped = false ; |
060 | super .onAttachedToWindow(); |
061 | mHandler = new Handler(); |
064 | * requests a tick on the next hard-second boundary |
066 | mTicker = new Runnable() { |
068 | if (mTickerStopped) return ; |
069 | mCalendar.setTimeInMillis(System.currentTimeMillis()); |
070 | setText(DateFormat.format(mFormat, mCalendar)); |
072 | long now = SystemClock.uptimeMillis(); |
073 | long next = now + ( 1000 - now % 1000 ); |
074 | mHandler.postAtTime(mTicker, next); |
081 | protected void onDetachedFromWindow() { |
082 | super .onDetachedFromWindow(); |
083 | mTickerStopped = true ; |
087 | * Pulls 12/24 mode from system settings |
089 | private boolean get24HourMode() { |
090 | return android.text.format.DateFormat.is24HourFormat(getContext()); |
093 | private void setFormat() { |
094 | if (get24HourMode()) { |
101 | private class FormatChangeObserver extends ContentObserver { |
102 | public FormatChangeObserver() { |
103 | super ( new Handler()); |
104 | }<a rel= "nofollow" >存为草稿</a> @Override |
105 | public void onChange( boolean selfChange) { |
五、使用方法
1 | < com.test.DigitalClock android:layout_x = "15dp" android:layout_y = "30dp" |
2 | android:layout_width = "wrap_content" android:layout_height = "wrap_content" |
3 | android:textColor = "<a href=" http://my.oschina.net/asia" target = "_blank" rel = "nofollow" >@android</ a > :color/white" android:textSize="55sp" |
4 | android:shadowColor="< a href = "http://my.oschina.net/asia" target = "_blank" rel = "nofollow" >@android</ a > :color/white" android:shadowRadius="2.0" |
6 | </ com.test.DigitalClock > |