Android Kotlin倒数计时器的时间添加

0 人关注

在我使用的定时器中,如何在按下按钮时增加一个时间? 例如,我想在按下按钮时,millisUntilFinished增加5秒。我试过用全局变量,但没有成功。

 object :CountDownTimer(10000,1000){
            override fun onFinish() {
                timeText.text = "Left : 0"
                handler.removeCallbacks(runnable)
                for (image in imageArray){
                    image.visibility = View.INVISIBLE
                for (add in timeAdd){
                    add.visibility = View.INVISIBLE
                button.visibility = View.VISIBLE
            override fun onTick(millisUntilFinished: Long) {
                 timeText.text = "Left : "+millisUntilFinished/1000
        }.start()
    
android
kotlin
countdowntimer
facar
facar
发布于 2018-10-04
4 个回答
Vector
Vector
发布于 2018-10-04
0 人赞同

下面是我们使用的一个倒计时器

fun message(msg:String){
    object : CountDownTimer(4000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            tvMsg.visibility = View.VISIBLE
            tvMsg.text = msg
        override fun onFinish() {
            tvMsg.visibility = View.INVISIBLE
            tvMsg.text = ""
    }.start()

而我们使用的是一个普通的计时器

    if (result) {
        etItemData.setText("")
        message("Record Removed")
        Timer().schedule(1000){
            thisACTIVITY()

Kotlin抱怨这一点,不知道为什么

BegYourPardon
BegYourPardon
发布于 2018-10-04
0 人赞同

除了Vector的回答,我还做了一个按钮,每1秒显示一次倒计时。我把Vector的答案放到一个函数中,然后在我的按钮被按下时调用它。希望这对大家有所帮助。在这个例子中,它从4秒开始倒数。

private fun countdown(){
    object : CountDownTimer(4000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            otp_resend.text = (millisUntilFinished / 1000).toString()
        override fun onFinish() {
            // do something after countdown is done ie. enable button, change color 
             otp_resend.text = "done!"
    }.start()
    
Dhaval Patel
Dhaval Patel
发布于 2018-10-04
0 人赞同

@流浪者 回答:你不能更新 millisUntilFinished ,因为在《中国》中没有这样的方法可用。 CountDownTimer 类。

为了更新定时器,你需要停止当前的定时器,并以更新的 millisInFuture 值启动新的定时器。下面是示例代码,它将帮助你实现你想要的东西。

    var timer: Timer?=null
    //Call this method to start timer on activity start
    private fun startTimer(){
        timer = Timer(10000);
        timer?.start()
    //Call this method to update the timer
    private fun updateTimer(){
        if(timer!=null) {
            val miliis = timer?.millisUntilFinished + TimeUnit.SECONDS.toMillis(5)
            //Here you need to maintain single instance for previous
            timer?.cancel()
            timer = Timer(miliis);
            timer?.start()
        }else{
            startTimer()
    inner class Timer(miliis:Long) : CountDownTimer(miliis,1000){
        var millisUntilFinished:Long = 0
        override fun onFinish() {
            timeText.text = "Left : 0"
            handler.removeCallbacks(runnable)
            for (image in imageArray){
                image.visibility = View.INVISIBLE
            for (add in timeAdd){
                add.visibility = View.INVISIBLE
            button.visibility = View.VISIBLE
        override fun onTick(millisUntilFinished: Long) {
            this.millisUntilFinished = millisUntilFinished
            timeText.text = "Left : "+millisUntilFinished/1000
    
TheWanderer
TheWanderer
发布于 2018-10-04
0 人赞同

你不能改变一个已经创建的CountDownTimer的剩余时间。