android Chronometer控件使用大全
程序员文章站
2022-03-09 10:29:30
目录1、序言chronometer 是android 官方提供的计时器、可实现正/倒计时、格式固定:00:00:00;2、使用如图xmlxml 简化写法如下:
1、序言
chronometer 是android 官方提供的计时器、可实现正/倒计时、格式固定:00:00:00;
2、使用
如图xml
xml 简化写法如下:
<chronometer android:id="@+id/tvendtime" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
3、发起倒计时
tvendtime.run { //格式化-> 距结束00:00:00 format = "距结束%s" //设置倒计时时间 countdowntimemill -》自定义 单位毫秒 base = countdowntimemill + systemclock.elapsedrealtime() //是否为倒计时、 iscountdown = true //倒计时监听 每隔一秒 setonchronometerticklistener { //倒计时结束 if (systemclock.elapsedrealtime() - it.base >= 0) { it.stop() return@setonchronometerticklistener // todo: 2021/9/16 处理业务逻辑 } } }
4、自定义格式化
tvendtime.run { //格式化-> 距结束00:00:00 format = "距结束%s" //设置倒计时时间 base = countdowntimemill + systemclock.elapsedrealtime() //是否为倒计时、 iscountdown = true //倒计时监听 每隔一秒 setonchronometerticklistener { //倒计时结束 if (systemclock.elapsedrealtime() - it.base >= 0) { it.stop() return@setonchronometerticklistener //自定义日期格式 如 1天 23:02:56 it.text = timeutils.formatsecondbymill(86400 * 1000) } } }
工具类:
object timeutils { /** * 发送消息日期格式 * * @param msgtimemillis * @return */ fun getmsgformattime(msgtimemillis: long): string? { val nowtime = date() val msgtime = date(msgtimemillis) val days = differentdays(msgtime, nowtime) // 早上、下午、晚上 1:40 val hourofday = dateutils.gethour(msgtime) val whens: string = when { hourofday >= 18 -> { //18-24 "晚上" } hourofday >= 13 -> { //13-18 "下午" } hourofday >= 11 -> { //11-13 "中午" } hourofday >= 5 -> { //5-11 "早上" } else -> { //0-5 "凌晨" } } return if (days < 1) { whens + " " + dateutils.format(msgtime, "hh:mm") } else { // 昨天 // return dateutils.format(new date(msgtimemillis), "yyyy年mm月dd日 ") + when + dateutils.format(new date(msgtimemillis), " hh:mm"); dateutils.format(date(msgtimemillis), "yyyy年mm月dd日 ") } } /** * date2比date1多的天数 * @param date1 * @param date2 * @return */ private fun differentdays(msgtime: date, nowtime: date): int { val cal1 = calendar.getinstance() cal1.time = msgtime val cal2 = calendar.getinstance() cal2.time = nowtime val day1 = cal1[calendar.day_of_year] val day2 = cal2[calendar.day_of_year] val year1 = cal1[calendar.year] val year2 = cal2[calendar.year] return if (year1 != year2) //同一年 { var timedistance = 0 for (i in year1 until year2) { timedistance += if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) //闰年 { 366 } else //不是闰年 { 365 } } timedistance + (day2 - day1) } else //不同年 { day2 - day1 } } /** * 格式化秒数 * des:%02d 长度不够2位的时前面补0 */ fun formatseconds(seconds: long): string? { return when { seconds <= 0 -> { "00:00" } seconds < 60 -> { string.format(locale.getdefault(), "00:%02d", seconds % 60) } seconds < 3600 -> { string.format(locale.getdefault(), "%02d:%02d", seconds / 60, seconds % 60) } else -> { string.format( locale.getdefault(), "%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60 ) } } } fun formatsecond(seconds: long): string? { return when { seconds <= 0 -> { "0分" } seconds < 60 -> { string.format(locale.getdefault(), "%02d秒", seconds % 60) } seconds < 3600 -> { string.format(locale.getdefault(), "%02d分%02d秒", seconds / 60, seconds % 60) } else -> { string.format( locale.getdefault(), "%02d时%02d分", seconds / 3600, seconds % 3600 / 60, ) } } } /** * 格式化日期 eg 天 时:分:秒 00:00:00 * @param dayunit 单位 默认不写 */ fun formatsecondbymill(milltime: long, dayunit: string = "天"): string? { //小于一天、单位毫秒 return if (milltime <= 86400 * 1000) { formatseconds(milltime / 1000) } else { val onedaymill = 86400 * 1000 val day = milltime / onedaymill val lastonedaymills = milltime - day * onedaymill "$day$dayunit ${formatseconds(lastonedaymills / 1000)}" } } }
注意:别再format上做文章、因为没用!!!
到此这篇关于android chronometer控件简单使用的文章就介绍到这了,更多相关android chronometer控件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!