前言

  关键字: 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文档翻译。

结束

   问题解决。

更多相关文章

  1. 打开和关闭WIFI 的代码出现了问题 android
  2. Android(安卓)assert 机制
  3. Google Android源代码结构
  4. android手写输入
  5. MPAndroidChart中使用 LineChart 踩得坑!
  6. Android--Activity运用实验
  7. android 1.6 launcher研究之修改worksapce上的屏数
  8. Android(安卓)Studio中读取文本文件内容方法
  9. android EditText 限制输入大小写,大写转小写

随机推荐

  1. 使用Android(安卓)Ant在编译时混淆
  2. Android子线程更新View的探索
  3. android 使用asynctask结合fragment更新U
  4. android telephony phone application
  5. Android的各种Toast .
  6. 【翻译】Android(安卓)Support Library S
  7. Android中Handler的使用方法——在子线程
  8. Android(安卓)studio --1-- 从普通Intell
  9. Android Studio——为什么说android UI操
  10. Android以后会胜出其他Linux的手机平台