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

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


当前回答

有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 }

其他回答

// 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();
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()

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

使用芬兰湾的科特林:

var timer = object: CountDownTimer(30000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            tvTimer.setText("seconds remaining: " + millisUntilFinished / 1000)
        }

        override fun onFinish() {
            tvTimer.setText("done!")
        }
    }
timer.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();
}

输出:01:30

new CountDownTimer(90000, 1000) {

            public void onTick(long duration) {
                //tTimer.setText("seconds remaining: " + millisUntilFinished / 1000);
                //here you can have your logic to set text to edittext resource id
                // Duration
                long Mmin = (duration / 1000) / 60;
                long Ssec = (duration / 1000) % 60;
                if (Ssec < 10) {
                    tTimer.setText("" + Mmin + ":0" + Ssec);
                } else tTimer.setText("" + Mmin + ":" + Ssec);
            }

            public void onFinish() {
                tTimer.setText("00:00");
            }

        }.start();