Android countdown timer display milliseconds?

雨燕双飞 提交于 2019-12-24 04:10:59

问题


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:


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


来源:https://stackoverflow.com/questions/14669752/android-countdown-timer-display-milliseconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!