在Android中将倒数计时器上的if else条件设置为小于10秒

时间:2017-07-16 09:33:07

标签: android if-statement countdowntimer

我遇到了一些问题,如果计时器小于10秒,我该如何尝试设置if else条件,它必须提醒用户他们还剩10秒?

以下是更容易参考的代码:

runTimer = new CountDownTimer(45000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String text = String.format(Locale.getDefault(), "%02d min: %02d sec",
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
                tvTime.setText(text);

            }

            @Override
            public void onFinish() {    
                runTimer.cancel();
            }
        }.start();

2 个答案:

答案 0 :(得分:2)

检查此代码:

runTimer = new CountDownTimer(45000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        String text;
        if (millisUntilFinished < 1000)
            text = String.format(Locale.getDefault(), "%02d min: %02d sec",
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
        tvTime.setText(text);
    }
    @Override
    public void onFinish() {
        runTimer.cancel();
    }
}.start();

答案 1 :(得分:1)

试试这个:

runTimer = new CountDownTimer(45000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished / 1000 == 10){

                 // SHOW MESSAGE HERE

            }

        }

        @Override
        public void onFinish() {    
            runTimer.cancel();
        }
    }.start();