[Android1.5]DigitalClock自定义日期输出格式
16lz
2021-12-04
前言
关键字: DigitalClock date format
Android的DigitalClock并没有设置输出格式的属性或方法,但是可以通过继承重写来实现,见正文部分代码。
正文
一、需求
修改时间输出格式为仅显示小时和分钟。
二、效果图
三、说明
通过看源码可知,只需修改以下两行代码其他全部复制过来即可:
private final static Stringm12 = " h:mm:ssaa " ;private final static Stringm24 = " 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{
CalendarmCalendar;
private final static Stringm12 = " h:mmaa " ; // h:mm:ssaa
private final static Stringm24 = " k:mm " ; // k:mm:ss
private FormatChangeObservermFormatChangeObserver;
private RunnablemTicker;
private HandlermHandler;
private boolean mTickerStopped = false ;
StringmFormat;
public DigitalClock(Contextcontext){
super (context);
initClock(context);
}
public DigitalClock(Contextcontext,AttributeSetattrs){
super (context,attrs);
initClock(context);
}
private void initClock(Contextcontext){
Resourcesr = 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();
/**
*requestsatickonthenexthard-secondboundary
*/
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 ;
}
/**
*Pulls12/24modefromsystemsettings
*/
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.DigitalClockandroid: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文档翻译。
结束
问题解决。
更多相关文章
- Android沉浸式状态栏最省事的解决方案
- Android(安卓)NDK
- Android高手进阶教程(二十五)之---Android(安卓)中的AIDL!!!
- Android特色开发之传感器和语音识别
- Android日志系统Logcat源代码简要分析
- Android(安卓)MVP设计模式介绍(附简单Demo下载)
- android实用代码片段
- Android(安卓)Handler 分析学习
- inflater在ListView中的使用