自定义Chronometer实现定时器

news/2024/7/7 21:45:50

概述

自定义Chronometer实现定时器,引用方便,操作简单。

详细

前言

Android开发过程中,计时控件是经常回使用到的,在Android控件库中有一个能快捷实现计时功能的控件,它就是Chronometer,今天我们基于它自定义实现一个方便调用的计时控件TimeChronometer
今天涉及知识有:

  1. Chronometer常用方法
  2. TimeChronometerActivity中使用
  3. 效果图和项目结构图

一. Chronometer 常用方法

Chronometer实质是一个TextView,其具备TextView特性,也有以下自己的特性:

    //设置是否倒计时
    setCountDown(boolean countDown)
    //获取是否倒计时
    isCountDown()
    //设置基准时间
    setBase(long base)
    //设置时间格式
    setFormat(String format)
    //设置定时器监听
    setOnChronometerTickListener(OnChronometerTickListener listener)
    //启动
    start()
    //停止
    stop()

需要注意的是ChronometersetFormat(String format)方法并不能改变时间显示格式,Chronometer显示格式为00:00:00

二. TimeChronometer 在 Activity 中使用

这里我继承Chronometer实现了一个方便调用的定时器TimeChronometer,下面给出
TimeChronometerActivity中使用过程。
先在Activity中引用控件:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".ui.MainActivity">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.08" />
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="计时"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_name" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="倒计时"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn" />
        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="重置"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn2" />
        <com.ktdemo.function.TimeChronometer
            android:id="@+id/timeChronometer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn3" />
    </androidx.constraintlayout.widget.ConstraintLayout>

然后在Activity中调用:

    @RequiresApi(Build.VERSION_CODES.N)
    class MainActivity : AppCompatActivity(), View.OnClickListener {
        private lateinit var mBingding: ActivityMainBinding
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            mBingding = ActivityMainBinding.inflate(layoutInflater)
            setContentView(mBingding.root)
            initData()
            setListener()
        }
        private var initData = {
            mBingding.tvName.text = "我是谁"
        }
        private var setListener = {
            LogUtil.i("====setListener=====")
            mBingding.btn.setOnClickListener(this)
            mBingding.btn2.setOnClickListener(this)
            mBingding.btn3.setOnClickListener(this)
        }
        override fun onClick(v: View) {
            when (v.id) {
                R.id.btn -> {
                    LogUtil.i("====我点击了=====")
                    mBingding.timeChronometer.countUp(10*1000,{
                        LogUtil.i("=======这是什么======")
                        Toast.makeText(this@MainActivity,"结束了?",Toast.LENGTH_SHORT).show()
                    })
                }
                R.id.btn2 -> {
                    LogUtil.i("====我点击了===2==")
                    mBingding.timeChronometer.countDown(10*1000,{
                        LogUtil.i("=======这是什么======")
                        Toast.makeText(this@MainActivity,"结束了?",Toast.LENGTH_SHORT).show()
                    })
                }
                R.id.btn3 -> {
                    LogUtil.i("====我点击了===3==")
                    mBingding.timeChronometer.reset()
                }
                else -> {
                }
            }
        }
    }

三. 效果图和项目结构图

ok,今天关于自定义Chronometer时间控件的代码就讲到这里了,谢谢大家。


http://lihuaxi.xjx100.cn/news/1490620.html

相关文章

SNN论文总结

Is SNN a great work ? Is SNN a convolutional work ? ANN的量化在SNN中是怎么体现的&#xff0c;和threshold有关系吗&#xff0c;threshold可训练和这个有关吗&#xff08;应该无关&#xff09; 解决过发放不发放的问题。 Intuation SNN编码方式 Image to spike patter…

Kuka机器人设计通用码垛程序

假设需要一个码垛程序, 从输送线抓到托盘, 托盘每层4个, 需要码5层, 可以用以下程序架构设计: 1, 再config中定义层数cengshu , 每层码垛的个数(码垛的次数)cishu , 每层的高度levelHeight , 码垛放置点的集合putPoint[,] ,预放点1集合prePut1[,], 预放点2集合prePut2[,] DEC…

国际站腾讯云容器镜像服务介绍!!

腾讯云容器镜像服务&#xff1a; 腾讯云的容器镜像服务Tencent Cloud Container Registry&#xff08;TCR&#xff09;旨在为开发者和企业供给高效、安全的容器镜像办理和存储平台&#xff0c;以处理容器镜像的保管、分发和办理问题。容器镜像是一种轻量级、可移植的软件包&…

【Python】python 打印本地代理

前言 在 Python 中&#xff0c;如果需要访问被墙的网站或者需要隐藏本机 IP 地址&#xff0c;可以通过使用代理服务器来实现。本文将介绍如何打印本地代理的方法&#xff0c;并提供相应的代码和实例。 一、什么是代理服务器 代理服务器&#xff08;Proxy Server&#xff09;是…

爬虫逆向实战(二十四)--某鸟记录中心

一、数据接口分析 主页地址&#xff1a;某鸟记录中心 1、抓包 通过抓包可以发现数据接口是front/record/search/page 2、判断是否有加密参数 请求参数是否加密&#xff1f; 通过查看“载荷”模块可以发现&#xff0c;请求参数是加密的 请求头是否加密&#xff1f; 通过查…

Web Components详解-Custom Elements

目录 引言 演变过程 概述 使用方式 创建标签 定义标签 使用标签 获取标签 异步定义标签 升级标签 完整案例 结语 相关代码 参考文章 引言 随着项目体量的增大&#xff0c;组件化和模块化的优势也愈发明显了&#xff0c;构建可重复使用、独立、可互操作的组件变得…

HDLBits-Verilog学习记录 | Verilog Language-Modules(2)

文章目录 25.Adder 1 | Module add26.Adder 2 | Module fadd27.Carry-select adder28.Adder-subtractor 25.Adder 1 | Module add practice&#xff1a; You are given a module add16 that performs a 16-bit addition. Instantiate two of them to create a 32-bit adder. O…

React 生命周期新旧对比

前言 React16.4版本之后使用了新的生命周期&#xff0c;它使用了一些新的生命周期钩子&#xff08;getDerivedStateFromProps、getSnapshotBeforeUpdate&#xff09;&#xff0c;并且即将废弃老版的3个生命周期钩子&#xff08;componentWillMount、componentWillReceiveProps…