我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。

此外,我如何才能保持它的更新,直到它达到零分零秒?


当前回答

如CountDownTimer的文档所示:

new CountDownTimer(30000, 1000) { onTick(long milliseconds untilfinished) { mTextField。setText("seconds remaining: " + msuntilfinished / 1000); //逻辑来设置EditText可以在这里 } onFinish() { mTextField.setText(“完成了!”); } } .start ();

其他回答

如CountDownTimer的文档所示:

new CountDownTimer(30000, 1000) { onTick(long milliseconds untilfinished) { mTextField。setText("seconds remaining: " + msuntilfinished / 1000); //逻辑来设置EditText可以在这里 } onFinish() { mTextField.setText(“完成了!”); } } .start ();

// the count down timer


 new  CountDownTimer(30000, 1000)
         {
        @Override
        public void onTick(long l) {

        }
        @Override
        public void onFinish() {
        
                //on finish the count down timer finsih        
                         }
          
            }
        }

    }.start();

如果您使用以下代码(如已接受的答案所述),

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
       //here you can have your logic to set text to edittext
    }

    public void onFinish() {
        mTextField.setText("done!");
    }

}.start();

如果不仔细清理引用,将导致使用此代码的活动实例的内存泄漏。

使用以下代码

//Declare timer
CountDownTimer cTimer = null;

//start timer function
void startTimer() {
    cTimer = new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
        }
        public void onFinish() {
        }
    };
    cTimer.start();
}


//cancel timer
void cancelTimer() {
    if(cTimer!=null)
        cTimer.cancel();
}

你需要调用cTtimer.cancel()每当onDestroy()/onDestroyView()在拥有的活动/片段被调用。

我用kotlin流实现了一个很酷的定时器方法,所以你可以在ViewModel中实现它

    var countDownInit = 30
    fun countDownTimer() = flow<Int> {
    var time = countDownInit
    emit(time)
    while (true){
        time--
        delay(1000L)
        countDownInit = time
        emit(time)
    }
}

然后在你的activity或fragment中像这样调用这个函数

lifecycleScope.launch {
        lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                 viewModel.countDownTimer().collect{time->
                      //and update UI 
                     //and for the finish section you can just use this
                     this.cancel()          
                 }
        }
}

现在在应用程序生命周期的暂停阶段会出现崩溃,你总是有最新的时间

接口方式。

import android.os.CountDownTimer;

/**
 * Created by saikiran on 07-03-2016.
 */
public class CountDownTimerCustom extends CountDownTimer {

    private TimeTickListener mTickListener;
    private TimeFinishListener mFinishListener;
    private long millisUntilFinished;

    public CountDownTimerCustom(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);

    }

    public void updateTickAndFinishListener(TimeTickListener tickListener) {
        mTickListener = tickListener;
    }

    public void updateFinishListner(TimeFinishListener listener) {
        mFinishListener = listener;
    }

    public long getCurrentMs() {
        return millisUntilFinished;
    }

    public int getCurrentSec() {
        return (int) millisUntilFinished / 1000;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        this.millisUntilFinished = millisUntilFinished;
        if (mTickListener != null)
            mTickListener.onTick(millisUntilFinished);
    }

    @Override
    public void onFinish() {
        if (mTickListener != null)
            mTickListener.onFinished();
        mFinishListener.onFinished();
    }


    public interface TimeTickListener {
        void onTick(long mMillisUntilFinished);

        void onFinished();
    }

    public interface TimeFinishListener {
        void onFinished();
    }
}