ARG*_*Geo 2 android countdowntimer kotlin
我有以下用于简单倒数计时器的 Kotlin 代码:
val thousand: Long = 1000
val timer = object: CountDownTimer(1800000, 1000) {
override fun onTick(millisUntilFinished: Long) {
var timeResult = millisUntilFinished/thousand
textTimer.text = "$timeResult"
}
override fun onFinish() {
textTimer.text = "Time is out"
}
}
timer.start()
textTimer.text = "$timer"
Run Code Online (Sandbox Code Playgroud)
如何格式化1800 seconds以30 min : 00 sec在科特林?
您可以通过整数除法和取模 ( %)获得数字,
并将格式设置为 2 位padStart():
val secs = 1800
val formatted = "${(secs / 60).toString().padStart(2, '0')} min : ${(secs % 60).toString().padStart(2, '0')} sec"
println(formatted)
Run Code Online (Sandbox Code Playgroud)
将打印
30 min : 00 sec
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |