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

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


当前回答

倒转倒计时计时器与小时,分钟和秒

public void reverseTimer(int Seconds, final TextView tv) {

    new CountDownTimer(Seconds * 1000 + 1000, 1000) {

        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000);

            int hours = seconds / (60 * 60);
            int tempMint = (seconds - (hours * 60 * 60));
            int minutes = tempMint / 60;
            seconds = tempMint - (minutes * 60);

            tv.setText("TIME : " + String.format("%02d", hours)
                    + ":" + String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));
        }

        public void onFinish() {
            tv.setText("Completed");
        }
    }.start();
}

其他回答

var futureMinDate = Date()
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
try {
    futureMinDate = sdf.parse("2019-08-22")
} catch (e: ParseException) {
    e.printStackTrace()
}

// Here futureMinDate.time Returns the number of milliseconds since January 1, 1970, 00:00:00 GM
// So we need to subtract the millis from current millis to get actual millis
object : CountDownTimer(futureMinDate.time - System.currentTimeMillis(), 1000) {
    override fun onTick(millisUntilFinished: Long) {
        val sec = (millisUntilFinished / 1000) % 60
        val min = (millisUntilFinished / (1000 * 60)) % 60
        val hr = (millisUntilFinished / (1000 * 60 * 60)) % 24
        val day = ((millisUntilFinished / (1000 * 60 * 60)) / 24).toInt()
        val formattedTimeStr = if (day > 1) "$day days $hr : $min : $sec"
        else "$day day $hr : $min : $sec"
        tvFlashDealCountDownTime.text = formattedTimeStr
    }

    override fun onFinish() {
        tvFlashDealCountDownTime.text = "Done!"
    }
}.start()

传递一个未来日期并将其转换为毫秒。

试试这个方法:

private void startTimer() {
    startTimer = new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {

            long sec = (TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));

            Log.e(TAG, "onTick: "+sec );
            tv_timer.setText(String.format("( %02d SEC )", sec));
            if(sec == 1)
            {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv_timer.setText("( 00 SEC )");
                    }
                }, 1000);
            }


        }

        public void onFinish() {
            tv_timer.setText("Timer finish");
        }
    }.start();
}

我用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()          
                 }
        }
}

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

有Rx的纯溶液,有人可能会感兴趣

数字扩展(Double, Float, Long等):

fun Number.countDownTimer(tick: Long = 1, timeUnit: TimeUnit = TimeUnit.SECONDS): Observable<Long> {
    val count = this.toLong()
    return Observable.interval(tick, timeUnit)
        .take(count)
        .map { count - it - 1 }
}

使用

60.countDownTimer().subscribe { textView.text = it }

倒转倒计时计时器与小时,分钟和秒

public void reverseTimer(int Seconds, final TextView tv) {

    new CountDownTimer(Seconds * 1000 + 1000, 1000) {

        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000);

            int hours = seconds / (60 * 60);
            int tempMint = (seconds - (hours * 60 * 60));
            int minutes = tempMint / 60;
            seconds = tempMint - (minutes * 60);

            tv.setText("TIME : " + String.format("%02d", hours)
                    + ":" + String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));
        }

        public void onFinish() {
            tv.setText("Completed");
        }
    }.start();
}