这篇文章主要介绍android中Chronometer控件怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Chronometer 是android 官方提供的计时器、可实现正/倒计时、格式固定:00:00:00;
如图xml
xml 简化写法如下:
<Chronometer android:id="@+id/tvEndTime" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
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 处理业务逻辑 } } }
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)}" } } }
以上是“android中Chronometer控件怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。