Android 自动更新时间的数字时钟 TextClock

TextClock 继承 TextView ,使用方法和 TextView 一样。

它专门用于显示数字时钟,可以自定义显示格式。

只要在布局文件里添加,它会自动更新时间,不需要添加刷新逻辑。

布局文件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="未设置Format:"
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <TextClock
            android:id="@+id/textclock0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#4B5C92"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yyyy/MM/dd EEEE HH:mm:ss : "
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <TextClock
            android:id="@+id/textclock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format12Hour="yyyy/MM/dd EEEE hh:mm:ss"
            android:format24Hour="yyyy/MM/dd EEEE HH:mm:ss"
            android:textColor="#4B5C92"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yyyy/M/dd EEE HH:mm:ss : "
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <TextClock
            android:id="@+id/textclock2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format12Hour="yyyy/M/dd EEE hh:mm:ss"
            android:format24Hour="yyyy/M/dd EEE HH:mm:ss"
            android:textColor="#4B5C92"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MM dd EEEE HH:mm : "
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <TextClock
            android:id="@+id/textclock3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format12Hour="MM dd EEEE hh:mm"
            android:format24Hour="MM dd EEEE HH:mm"
            android:textColor="#4B5C92"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MMM dd EEE HH:mm :"
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <TextClock
            android:id="@+id/textclock4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format12Hour="MMM dd EEE HH:mm"
            android:format24Hour="MMM dd EEE HH:mm"
            android:textColor="#4B5C92"
            android:textSize="30sp" />
    </LinearLayout>

</LinearLayout>

实际效果,
中文,24小时制
在这里插入图片描述

总结:

如果不设置 android:format12Hour 、android:format24Hour ,默认以 12:48 格式显示。

如果设置了 format ,参考表格

格式 效果
yyyy/M/dd 2024/6/25
yyyy/MM/dd 2024/06/25
MMM 6月
EEE 中文显示周几;英文显示星期缩写,如 Thu
EEEE 中文显示星期几;英文显示星期完整拼写,如 Thurday
HH:mm:ss 15:32:16
hh:mm:ss 03:32:16
a 是否显示 AM/PM

相关推荐

  1. mybatisplus实现自动创建/更新时间

    2024-07-12 07:54:04       53 阅读
  2. TextBlock run元素

    2024-07-12 07:54:04       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 07:54:04       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 07:54:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 07:54:04       43 阅读
  4. Python语言-面向对象

    2024-07-12 07:54:04       54 阅读

热门阅读

  1. 初识c++

    初识c++

    2024-07-12 07:54:04      20 阅读
  2. HTTP有哪些失败原因?怎么处理?

    2024-07-12 07:54:04       23 阅读
  3. 小白学webgl合集-import.meta.url 和 new URL() bug

    2024-07-12 07:54:04       27 阅读
  4. Excel中用VBA实现Outlook发送当前工作簿

    2024-07-12 07:54:04       26 阅读
  5. Openresty+lua 定时函数 ngx.timer.every

    2024-07-12 07:54:04       18 阅读
  6. 1.Introduction to Spring Web MVC framework

    2024-07-12 07:54:04       25 阅读
  7. 【layui表单赋值为空问题查找】

    2024-07-12 07:54:04       20 阅读
  8. 云原生(Cloud native)

    2024-07-12 07:54:04       21 阅读
  9. linux中如何设置多个redis进程并且设置独立密码?

    2024-07-12 07:54:04       21 阅读
  10. 使用SQL判断两表的关联关系

    2024-07-12 07:54:04       25 阅读
  11. yolo8识别图像中人物的个数

    2024-07-12 07:54:04       20 阅读