news 2026/4/18 11:06:32

Android 基础入门教程 Date Time组件(上)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android 基础入门教程 Date Time组件(上)

2.4.2 Date & Time组件(上)

分类Android 基础入门教程

本节引言:

本节给大家带来的是Android给我们提供的显示时间的几个控件,他们分别是: TextClock,AnalogClock,Chronometer,另外其实还有个过时的DigitalClock就不讲解了! 好的,开始本节内容!


1.TextClock(文本时钟)

TextClock是在Android 4.2(API 17)后推出的用来替代DigitalClock的一个控件!
TextClock可以以字符串格式显示当前的日期和时间,因此推荐在Android 4.2以后使用TextClock。
这个控件推荐在24进制的android系统中使用,TextClock提供了两种不同的格式, 一种是在24进制中显示时间和日期,另一种是在12进制中显示时间和日期。大部分人喜欢默认的设置。

可以通过调用:TextClock提供的is24HourModeEnabled()方法来查看,系统是否在使用24进制时间显示! 在24进制模式中:

  • 如果没获取时间,首先通过getFormat24Hour()返回值;
  • 获取失败则通过getFormat12Hour()获取返回值;
  • 以上都获取失败则使用默认;

另外他给我们提供了下面这些方法,对应的还有get方法:

Attribute NameRelated MethodDescription
android:format12HoursetFormat12Hour(CharSequence)设置12时制的格式
android:format24HoursetFormat24Hour(CharSequence)设置24时制的格式
android:timeZonesetTimeZone(String)设置时区

其实更多的时间我们是花在时间形式定义上,就是里面这个CharSequence! 这里提供下常用的写法以及结果:

<TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MM/dd/yy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MMM dd, yyyy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="MMMM dd, yyyy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="E, MMMM dd, yyyy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:format12Hour="Noteworthy day: 'M/d/yy"/>

运行结果:

PS:另外minsdk 要大于或者等于17哦!


2.AnalogClock(模拟时钟)

就是下图这种:

官网中我们可以看到这样三个属性:

依次是:表背景,表时针,分时针的图片,我们可以自行定制:

示例代码如下:

<AnalogClock android:layout_width="100dp" android:layout_height="100dp" android:dial="@mipmap/ic_c_bg" android:hand_hour="@mipmap/zhen_shi" android:hand_minute="@mipmap/zhen_fen" />

运行结果:


3.Chronometer(计时器)

如题,就是一个简单的计时器,我们直接上使用示例吧:

使用示例:

实现代码:

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Chronometer android:id="@+id/chronometer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#ff0000" android:textSize="60dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:orientation="horizontal"> <Button android:id="@+id/btnStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="开始记时" /> <Button android:id="@+id/btnStop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止记时" /> <Button android:id="@+id/btnReset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="重置" /> <Button android:id="@+id/btn_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="格式化" /> </LinearLayout> </LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{ private Chronometer chronometer; private Button btn_start,btn_stop,btn_base,btn_format; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { chronometer = (Chronometer) findViewById(R.id.chronometer); btn_start = (Button) findViewById(R.id.btnStart); btn_stop = (Button) findViewById(R.id.btnStop); btn_base = (Button) findViewById(R.id.btnReset); btn_format = (Button) findViewById(R.id.btn_format); chronometer.setOnChronometerTickListener(this); btn_start.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_base.setOnClickListener(this); btn_format.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnStart: chronometer.start();// 开始计时 break; case R.id.btnStop: chronometer.stop();// 停止计时 break; case R.id.btnReset: chronometer.setBase(SystemClock.elapsedRealtime());// 复位 break; case R.id.btn_format: chronometer.setFormat("Time:%s");// 更改时间显示格式 break; } } @Override public void onChronometerTick(Chronometer chronometer) { String time = chronometer.getText().toString(); if(time.equals("00:00")){ Toast.makeText(MainActivity.this,"时间到了~",Toast.LENGTH_SHORT).show(); } } }

运行截图:


本节小结:

本节跟大家简单的介绍了TextClock,AnalogClock,Chronometer这三个组件,从篇幅就可以看出 其实这几个东西用得并不多,几乎是没用过...知道下就好,用法也超简单... 就这样吧,本节就到这里~谢谢

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 5:27:05

Open-AutoGLM项目实战:在Android设备上实现自动操作与ADB键盘控制

项目简介 本文将详细介绍如何使用Open-AutoGLM项目实现对Android设备的自动控制&#xff0c;包括ADB键盘配置和设备连接设置。 ⚠️ 本文章仅供研究和学习使用。严禁用于非法获取信息、干扰系统或任何违法活动。请仔细审阅 使用条款 1. 项目获取与安装 首先&#xff0c;克隆项…

作者头像 李华
网站建设 2026/4/18 8:16:06

30人左右的小企业如何做文件共享?实现文件高效管理

企业团队紧凑、业务灵活&#xff0c;却常被企业文件管理的 “小事” 绊住脚&#xff1a;有人在微信群里翻半小时找历史合同&#xff0c;有人因本地硬盘损坏导致客户资料归零。那么&#xff0c;30人左右的小企业如何优雅地解决文件共享难题&#xff1f; 一、30人小企业文件共享…

作者头像 李华
网站建设 2026/4/1 14:55:35

大模型工程师月薪4万+!国家战略护航,500万人才缺口,小白也能入门的AI学习指南_AI大模型架构师月薪均4.6万,人才缺口500万!

人工智能领域&#xff0c;特别是大模型相关岗位薪资高企&#xff08;AI架构师月薪超4万&#xff09;&#xff0c;国家政策大力支持&#xff0c;人才缺口达500万。人工智能是多学科交叉专业&#xff0c;核心课程包括机器学习、深度学习等。开设院校众多&#xff0c;可按顶配资源…

作者头像 李华
网站建设 2026/4/18 0:27:07

低代码平台Python插件开发指南(仅限高级工程师掌握的机密方案)

第一章&#xff1a;低代码平台Python插件开发的核心价值在现代软件开发中&#xff0c;低代码平台通过可视化界面显著提升了应用构建效率。然而&#xff0c;面对复杂业务逻辑或特定功能需求时&#xff0c;平台内置组件往往难以满足全部场景。此时&#xff0c;Python插件开发成为…

作者头像 李华
网站建设 2026/4/18 4:10:53

中断导致数据 corruption?,立即掌握C语言环境下的4步防护机制

第一章&#xff1a;中断导致数据 corruption 的根源剖析在现代操作系统和嵌入式系统中&#xff0c;中断是实现异步事件响应的核心机制。然而&#xff0c;若中断处理不当&#xff0c;极易引发数据 corruption&#xff0c;尤其是在共享资源未加保护的场景下。这类问题通常难以复现…

作者头像 李华
网站建设 2026/4/18 6:30:47

为什么90%的边缘AI项目失败?Python部署避坑指南来了

第一章&#xff1a;边缘AI项目失败的根源剖析在边缘AI项目的实施过程中&#xff0c;许多团队面临性能不达预期、部署失败或维护成本过高的问题。这些问题往往并非源于单一技术缺陷&#xff0c;而是由多个系统性因素交织导致。硬件与模型不匹配 边缘设备资源有限&#xff0c;而部…

作者头像 李华