Android countdown timer display milliseconds?

2019-07-18 12:11发布

I want a timer to pop up when I click a button that will count down from 3 seconds. And it does that fine but i want it to also show the milliseconds so when I click the button the text would go from 3.0 to 0.1. How would I add the milliseconds to the text view?

new CountDownTimer(1000, 3000) {

                public void onTick(long millisUntilFinished) {
                    textViewTimer.setText("" + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    textViewTimer.setVisibility(View.INVISIBLE);
                    textViewLevelGained.setVisibility(View.INVISIBLE);

                }
            }.start();

This is what I have

1条回答
迷人小祖宗
2楼-- · 2019-07-18 12:21

Other SO questions suggest CountDownTimer doesn't do sub 1-second granularity well. Look into a different class, like TimerTask.

Otherwise, the following would work.

new CountDownTimer(3000, 1) {
    public void onTick(long millisUntilFinished) {
        textViewTimer.setText("" + millisUntilFinished / 1000
          + "." + millisUntilFinished % 1000);
    }

    public void onFinish() {
        textViewTimer.setVisibility(View.INVISIBLE);
        textViewLevelGained.setVisibility(View.INVISIBLE);
    }
}.start();
查看更多
登录 后发表回答