• Android学习笔记 2.3.7 时钟(AnalogClock和TextClock)的功能与用法 && 2.3.8 计时器(Chronometer)


    Android学习笔记

    疯狂Android讲义

    第2章 Android 应用的界面编程

    2.3 第2组 UI组件:TextView及其子类

    2.3.7 时钟(AnalogClock和TextClock)的功能与用法

    时钟UI组件是两个非常简单的组件:TextClock本身就继承了TextView-
    也就是说,它本身就是文本框,只是它里面显示的内容总是当前时间。与TextView 不同的是,为TextClock 设置android:text属性没什么作用。

    TextClock取代早期的 DigitalClock 组件,因此功能更加强大——TextClock能以24小时制或12小时制来显示时间,而且可以由程序员来指定时间格式

    TextClock支持的XML属性及相关方法:

    在这里插入图片描述

    AnalogClock则继承了View组件,它重写了View的OnDraw()方法,它会在View上绘制模拟时钟。

    AnalogClock支持的XML属性

    在这里插入图片描述

    TextClock和 AnalogClock 都会显示当前时间。不同的是,TextClock 显示数字时钟,可以显示当前的秒数; AnalogClock 显示模拟时钟,不会显示当前的秒数。

    实例——手机里的“劳力士”

    创建新模块

    在这里插入图片描述

    布局

    
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        
        <AnalogClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        
        <TextClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableEnd="@mipmap/ic_launcher"
            android:format12Hour="yyyy年MM月dd日 H:mma EEEE"
            android:textColor="#f0f"
            android:textSize="20dp" />
    
        
        <AnalogClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:dial="@drawable/watch"
            android:hand_minute="@drawable/hand" />
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    直接运行

    在这里插入图片描述

    2.3.8 计时器(Chronometer)

    Android还提供了一个计时器组件:Chronometer,该组件与TextClock都继承自TextView,因此它们都会显示一段文本。但 Chronometer 并不显示当前时间,它显示的是从某个起始时间开始,一共过去了多长时间。

    Chronometer的用法也很简单,它只提供了android:format和 android:countDown属性,其中前者用于指定计时器的计时格式。除此之外,Chronometer还支持如下常用方法:

    • setBase(long base):设置计时器的起始时间。
    • setFormat(String format):设置显示时间的格式。
    • start():开始计时。
    • stop():停止计时。
    • setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):为计时器绑定事件监听器,当计时器改变时触发该监听器。

    新建模块

    在这里插入图片描述

    布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">
        <Chronometer
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12pt"
            android:textColor="#ffff0000"/>
        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="启动"/>
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    逻辑代码

    package com.dingjiaxiong.chronometertest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.database.CursorJoiner;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.widget.Button;
    import android.widget.Chronometer;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //获取组件
            Chronometer ch = findViewById(R.id.test);
    
            //获取按钮
            Button start = findViewById(R.id.start);
    
            start.setOnClickListener((view) ->{
                //设置开始计时时间
                ch.setBase(SystemClock.elapsedRealtime());
                //启动计时器
                ch.start();
                start.setEnabled(false);
            });
    
            //为Chronmeter绑定事件监听器
            ch.setOnChronometerTickListener((source) ->{
                //如果从开始计时到现在超过了20s
                if (SystemClock.elapsedRealtime() - ch.getBase() > 5 * 1000){
                    ch.stop();
                    start.setEnabled(true);
                }
            });
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    运行效果

    在这里插入图片描述

  • 相关阅读:
    搭建web网站
    leecode#查找重复的电子邮箱#从不订购的客户#颠倒二进制数#位1的个数
    asp.net core mvc之 视图
    Sharding JDBC案例实战
    九种常见UML图
    Flink-使用流批一体API统计单词数量
    源码解读etcd heartbeat,election timeout之间的拉锯
    javascript算法排序之希尔排序
    SpringCloud总结
    Mach Systems—总线接口转换工具
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126458860