In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the Chronometer control in android, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
1. Preface
Chronometer is officially provided by android timer, can achieve forward / countdown, fixed format: 00:00:00
2. Use
As shown in the picture, xml
The simplified writing of xml is as follows:
3. Start the countdown tvEndTime.run {/ / format-> from the end 00:00:00 format = "% s" / / set the countdown time countDownTimeMill-"Custom unit millisecond base = countDownTimeMill + SystemClock.elapsedRealtime () / / whether it is the countdown, IsCountDown = true / / countdown monitoring ends every second setOnChronometerTickListener {/ / countdown ends if (SystemClock.elapsedRealtime ()-it.base > = 0) {it.stop () return@setOnChronometerTickListener / / TODO: 2021max 9 / 16 processing business logic} 4. Custom format tvEndTime.run {/ / format-> from the end 00:00:00 format = "from the end% s" / / set the countdown base = countDownTimeMill + SystemClock.elapsedRealtime () / / whether it is the countdown, IsCountDown = true / / countdown monitoring every second setOnChronometerTickListener {/ / countdown ends if (SystemClock.elapsedRealtime ()-it.base > = 0) {it.stop () return@setOnChronometerTickListener / / Custom date grid The formula is like 23:02:56 it.text = TimeUtils.formatSecondByMill (86400 * 1000)}.
Tool class:
Object TimeUtils {/ * send message date format * * @ param msgTimeMillis * @ return * / fun getMsgFormatTime (msgTimeMillis: Long): String? {val nowTime = Date () val msgTime = Date (msgTimeMillis) val days = differentDays (msgTime) NowTime) / / in the morning, afternoon, 1:40 evening val hourOfDay = DateUtils.getHour (msgTime) val whens: String = when {hourOfDay > = 18-> {/ / 18-24 "evening"} hourOfDay > = 13-> {/ / 13-18 "afternoon"} hourOfDay > = 11-> {/ / 11-13 "noon"} hourOfDay > = 5-> {/ / 5-11 "morning"} else-> {/ / 0-5 "morning"}} 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 = msgTimeval cal2 = Calendar.getInstance() cal2.time = nowTimeval 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:d 长度不够2位的时前面补0 */ fun formatSeconds(seconds: Long): String? { return when { seconds { "00:00" } seconds < 60 ->{String.format (Locale.getDefault (), "00seconds d", seconds% 60)} seconds
< 3600 ->{String.format (Locale.getDefault (), "seconds d", seconds / 60, seconds% 60)} else-> {String.format (Locale.getDefault (), "d:d:d", seconds / 3600, seconds% 3600 / 60) Seconds% 60)}} fun formatSecond (seconds: Long): String? {return when {seconds {"0"} seconds
< 60 ->{String.format (Locale.getDefault (), "d seconds", seconds% 60)} seconds
< 3600 ->{String.format (Locale.getDefault (), "d minutes d seconds", seconds / 60, seconds% 60)} else-> {String.format (Locale.getDefault (), "d hours and d minutes", seconds / 3600, seconds% 3600 / 60) )}} / * format date eg day hours: minutes: 00:00:00 seconds * @ param dayUnit units do not write * / fun formatSecondByMill (millTime: Long, dayUnit: String = "days"): String? {/ / less than one day, unit millisecond return if (millTime)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.