用kotlin写一个计时器
时间: 2023-08-29 10:06:00 浏览: 57
好的,下面是一个简单的计时器示例:
```kotlin
import java.util.*
import kotlin.concurrent.timer
fun main() {
val scanner = Scanner(System.`in`)
println("请输入计时时间(单位:秒):")
val seconds = scanner.nextInt()
var count = seconds
val timerTask = timer(period = 1000) {
count--
if (count <= 0) {
cancel()
} else {
println("还剩 $count 秒")
}
}
}
```
这个程序会要求用户输入计时时间(以秒为单位),然后开始倒计时。每秒钟会输出当前剩余时间,直到计时结束。在这个示例中,我们使用了 `java.util.Timer` 类来实现计时器,每秒钟执行一次计时任务。
相关问题
kotlin 计时器
Kotlin是一种现代化的静态类型编程语,它运行在Java虚拟机上且可以与Java代码无缝地互操作。Kotlin提供丰富的标准库和语法糖,使得开发者可以更加简洁和高效地编写代码。
在Kotlin中,你可以使用定时器来执行一些定时任务。常见的定时器类是`Timer`和`ScheduledThreadPoolExecutor`。
1. 使用`Timer`类:
```kotlin
import java.util.*
fun main() {
val timer = Timer()
val delay = 1000L // 延迟1秒
val period = 2000L // 每2秒执行一次
timer.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
// 在这里编写你要执行的任务
println("定时任务执行了")
}
}, delay, period)
}
```
2. 使用`ScheduledThreadPoolExecutor`类:
```kotlin
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
fun main() {
val executor = Executors.newScheduledThreadPool(1)
val delay = 1000L // 延迟1秒
val period = 2000L // 每2秒执行一次
executor.scheduleAtFixedRate({
// 在这里编写你要执行的任务
println("定时任务执行了")
}, delay, period, TimeUnit.MILLISECONDS)
}
```
以上两种方式都可以实现定时任务的执行,你可以根据自己的需求选择适合的方式。
用kotlin的写法写一个 CountDownTimer
好的,以下是用Kotlin写一个CountDownTimer的示例代码:
```
class MyCountDownTimer(val millisInFuture: Long, val countDownInterval: Long) {
private var isTimerRunning = false
private var timeLeft: Long = millisInFuture
private var timer: CountDownTimer? = null
private var onTickListener: ((Long) -> Unit)? = null
private var onFinishListener: (() -> Unit)? = null
fun start() {
if (!isTimerRunning) {
timer = object : CountDownTimer(timeLeft, countDownInterval) {
override fun onTick(millisUntilFinished: Long) {
timeLeft = millisUntilFinished
onTickListener?.invoke(millisUntilFinished)
}
override fun onFinish() {
isTimerRunning = false
onFinishListener?.invoke()
}
}.start()
isTimerRunning = true
}
}
fun cancel() {
timer?.cancel()
isTimerRunning = false
}
fun setTimeLeft(timeLeft: Long) {
this.timeLeft = timeLeft
}
fun setOnTickListener(onTickListener: (Long) -> Unit) {
this.onTickListener = onTickListener
}
fun setOnFinishListener(onFinishListener: () -> Unit) {
this.onFinishListener = onFinishListener
}
}
```
使用方法如下:
```
val myCountDownTimer = MyCountDownTimer(60000, 1000)
myCountDownTimer.setOnTickListener { millisUntilFinished ->
// Update UI with time left
}
myCountDownTimer.setOnFinishListener {
// Do something when timer finishes
}
myCountDownTimer.start()
```
这个CountDownTimer会持续60秒,并且每隔1秒回调一次onTickListener,最后结束时调用onFinishListener。如果需要修改时间,可以调用setTimeLeft方法。如果需要停止计时器,可以调用cancel方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)