Android的天文台启动和停止,但停​​止时进行计数 [英] Android Chronometer starts and stops but carries on counting when stopped

查看:262
本文介绍了Android的天文台启动和停止,但停​​止时进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个有一个切换按钮来启动和停止天文台表,就像秒表一样的应用程序。当你第一次preSS切换按钮(执行时。开始()),记时计将开始计时,当你再preSS一次按钮(执行时 .stop()),记时计在屏幕上出现停止计数,因为它应该,但是当它然后重新启动它表明,它实际上仍是在后台计算并显示时间在屏幕上,就好像从来没有停止过搜索结果

code:结果

  tglStartStop.setOnCheckedChangeListener(新OnCheckedChangeListener(){        @覆盖
        公共无效onCheckedChanged(CompoundButton buttonView,
                布尔器isChecked){            如果(tglStartStop.isChecked()){                chronStopwatch.start();
            }            其他{                chronStopwatch.stop();
            }        }    });


解决方案

添加此行之前(chronStopwatch.start();):

  chronStopwatch.setBase(SystemClock.elapsedRealtime());

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

        }

    });

解决方案

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

chronStopwatch.setBase(SystemClock.elapsedRealtime());

这篇关于Android的天文台启动和停止,但停​​止时进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆