Android DigitalClock自定义日期输出格式

Android的DigitalClock并没有设置输出格式的属性或方法,但是可以通过继承重写来实现,见正文部分代码。

正文

  一、需求

    修改时间输出格式为仅显示小时和分钟。

  二、效果图

三、说明

    通过看源码可知,只需修改以下两行代码其他全部复制过来即可:

1 private final static String m12 = "h:mm:ss aa";
2 private final static String m24 = "k:mm:ss";

如果想做得更好更通用的话可以把设置日期格式的方法暴露出来,或者为其增加一个xml属性。

  四、完整代码

001 package com.test;
002  
003 import java.util.Calendar;
004  
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;
013  
014 /**
015  * 自定义DigitalClock输出格式
016  * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a>  农民伯伯
017  *
018  */
019 public class DigitalClock extends android.widget.DigitalClock {
020  
021     Calendar mCalendar;
022     private final static String m12 = "h:mm aa";//h:mm:ss aa
023     private final static String m24 = "k:mm";//k:mm:ss
024     private FormatChangeObserver mFormatChangeObserver;
025  
026     private Runnable mTicker;
027     private Handler mHandler;
028  
029     private boolean mTickerStopped = false;
030  
031     String mFormat;
032  
033     public DigitalClock(Context context) {
034         super(context);
035         initClock(context);
036     }
037  
038     public DigitalClock(Context context, AttributeSet attrs) {
039         super(context, attrs);
040         initClock(context);
041     }
042  
043     private void initClock(Context context) {
044         Resources r = context.getResources();
045  
046         if (mCalendar == null) {
047             mCalendar = Calendar.getInstance();
048         }
049  
050         mFormatChangeObserver = new FormatChangeObserver();
051         getContext().getContentResolver().registerContentObserver(
052                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
053  
054         setFormat();
055     }
056  
057     @Override
058     protected void onAttachedToWindow() {
059         mTickerStopped = false;
060         super.onAttachedToWindow();
061         mHandler = new Handler();
062  
063         /**
064          * requests a tick on the next hard-second boundary
065          */
066         mTicker = new Runnable() {
067                 public void run() {
068                     if (mTickerStopped) return;
069                     mCalendar.setTimeInMillis(System.currentTimeMillis());
070                     setText(DateFormat.format(mFormat, mCalendar));
071                     invalidate();
072                     long now = SystemClock.uptimeMillis();
073                     long next = now + (1000 - now % 1000);
074                     mHandler.postAtTime(mTicker, next);
075                 }
076             };
077         mTicker.run();
078     }
079  
080     @Override
081     protected void onDetachedFromWindow() {
082         super.onDetachedFromWindow();
083         mTickerStopped = true;
084     }
085  
086     /**
087      * Pulls 12/24 mode from system settings
088      */
089     private boolean get24HourMode() {
090         return android.text.format.DateFormat.is24HourFormat(getContext());
091     }
092  
093     private void setFormat() {
094         if (get24HourMode()) {
095             mFormat = m24;
096         else {
097             mFormat = m12;
098         }
099     }
100  
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) {
106             setFormat();
107         }
108     }
109 }

五、使用方法

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"
5             >
6 </com.test.DigitalClock>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java编程实现数字时钟的示例代码,包括多种格式输出时间信息: ```java import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class DigitalClock { public static void main(String[] args) { while (true) { LocalTime now = LocalTime.now(); System.out.println("Current Time: " + now); // Format 1: hh:mm:ss DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("HH:mm:ss"); String time1 = now.format(formatter1); System.out.println("Time Format 1: " + time1); // Format 2: h:mm:ss a DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("h:mm:ss a"); String time2 = now.format(formatter2); System.out.println("Time Format 2: " + time2); // Format 3: HH:mm DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm"); String time3 = now.format(formatter3); System.out.println("Time Format 3: " + time3); // Format 4: h:mm a DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("h:mm a"); String time4 = now.format(formatter4); System.out.println("Time Format 4: " + time4); try { Thread.sleep(1000); // Wait for 1 second } catch (InterruptedException e) { e.printStackTrace(); } // Clear console System.out.print("\033[H\033[2J"); System.out.flush(); } } } ``` 代码解释: - `LocalTime.now()` 获取当前时间对象; - `DateTimeFormatter` 定义时间格式; - `now.format(formatter)` 将时间对象格式化为指定格式的字符串; - `Thread.sleep(1000)` 使程序等待 1 秒钟; - `System.out.print("\033[H\033[2J")` 和 `System.out.flush()` 清空控制台输出。 运行示例代码,会不断输出当前时间和多种格式的时间信息,类似于数字时钟效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值