您好,登錄后才能下訂單哦!
這篇文章主要介紹android中Chronometer控件怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
Chronometer 是android 官方提供的計(jì)時(shí)器、可實(shí)現(xiàn)正/倒計(jì)時(shí)、格式固定:00:00:00;
如圖xml
xml 簡(jiǎn)化寫法如下:
<Chronometer android:id="@+id/tvEndTime" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
tvEndTime.run { //格式化-> 距結(jié)束00:00:00 format = "距結(jié)束%s" //設(shè)置倒計(jì)時(shí)時(shí)間 countDownTimeMill -》自定義 單位毫秒 base = countDownTimeMill + SystemClock.elapsedRealtime() //是否為倒計(jì)時(shí)、 isCountDown = true //倒計(jì)時(shí)監(jiān)聽 每隔一秒 setOnChronometerTickListener { //倒計(jì)時(shí)結(jié)束 if (SystemClock.elapsedRealtime() - it.base >= 0) { it.stop() return@setOnChronometerTickListener // TODO: 2021/9/16 處理業(yè)務(wù)邏輯 } } }
tvEndTime.run { //格式化-> 距結(jié)束00:00:00 format = "距結(jié)束%s" //設(shè)置倒計(jì)時(shí)時(shí)間 base = countDownTimeMill + SystemClock.elapsedRealtime() //是否為倒計(jì)時(shí)、 isCountDown = true //倒計(jì)時(shí)監(jiān)聽 每隔一秒 setOnChronometerTickListener { //倒計(jì)時(shí)結(jié)束 if (SystemClock.elapsedRealtime() - it.base >= 0) { it.stop() return@setOnChronometerTickListener //自定義日期格式 如 1天 23:02:56 it.text = TimeUtils.formatSecondByMill(86400 * 1000) } } }
工具類:
object TimeUtils { /** * 發(fā)送消息日期格式 * * @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多的天數(shù) * @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 } } /** * 格式化秒數(shù) * des:%02d 長(zhǎng)度不夠2位的時(shí)前面補(bǔ)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時(shí)%02d分", seconds / 3600, seconds % 3600 / 60, ) } } } /** * 格式化日期 eg 天 時(shí):分:秒 00:00:00 * @param dayUnit 單位 默認(rèn)不寫 */ 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控件怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。