Android Chronometer starts and stops but carries on counting when stopped

我是研究僧i 提交于 2019-12-11 03:18:29

问题


I've created an app that has a toggle button to start and stop a Chronometer, like a stop watch. When you first press the toggle button (executes .start()), the chronometer will start counting, when you then press the button again (executes .stop()), the chronometer appears on screen to stop counting as it should but when it is then started again it shows that it's actually still been counting in the background and shows the time on screen as if it was never stopped

code:

tglStartStop.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (tglStartStop.isChecked()) {

                chronStopwatch.start();
            }

            else {

                chronStopwatch.stop();
            }

        }

    });

回答1:


Add this line before "chronStopwatch.start();"

chronStopwatch.setBase(SystemClock.elapsedRealtime());


来源:https://stackoverflow.com/questions/27859380/android-chronometer-starts-and-stops-but-carries-on-counting-when-stopped

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