天文台重置

发布于 2024-10-24 19:27:18 字数 1502 浏览 4 评论 0原文

我正在尝试完全重新启动 Chronometer,但它不起作用。相反,它正在暂停。基本上我想做的是在计时器计数到 10 时做一些事情。完成后我们提示用户重试。在这种情况下,我们希望从 1 秒重新计数到 10 秒。但是计时器从暂停时间开始,而不是从 0 开始。

代码如下:

_cutOfTime = 10; // constant

每次按下按钮时,startRecording()

都应该启动计时器,而不是停止/暂停它,但确实如此相反

protected void startRecording(){    

this._watch = (Chronometer) findViewById(R.id.chrono);

            if (this._watch != null)
                 this._watch.setOnChronometerTickListener(new OnChronometerTickListener() {

                        @Override
                        public void onChronometerTick(Chronometer chronometer) {
                            long countUp = (SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000;
                            Log.i(_tag, "time now: " + String.valueOf(countUp));

                            if(countUp > _cutOfTime)
                            {
                                Log.i(_tag, "stop recording!!: ");
                                 _watch.stop();
                                stopRecordWav();
                                launchPromptWithResults();
                            }
                            long sec = countUp % 60;

                            String asText = "" + sec;
                            _textView.setText("Recorded: " + asText);
                        }
                    });


            if (_watch != null)
            _watch.start();
}

有没有办法重置计时器,使其不暂停而是完全停止?

I am trying to completely restart Chronometer and its does not work. Instead it is being paused. Basically what I am trying to do is to do something while chronometer is counting till 10. After its done we prompt the user to try again. In which case we want to redo the count from 1 to 10 sec. But the Chronometer starts from the paused time instead of starting 0.

here is the code:

_cutOfTime = 10; // constant

every time button is pressed do startRecording()

it should always initiate the Chronometer instead of stop/pause it, but it does the opposite

protected void startRecording(){    

this._watch = (Chronometer) findViewById(R.id.chrono);

            if (this._watch != null)
                 this._watch.setOnChronometerTickListener(new OnChronometerTickListener() {

                        @Override
                        public void onChronometerTick(Chronometer chronometer) {
                            long countUp = (SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000;
                            Log.i(_tag, "time now: " + String.valueOf(countUp));

                            if(countUp > _cutOfTime)
                            {
                                Log.i(_tag, "stop recording!!: ");
                                 _watch.stop();
                                stopRecordWav();
                                launchPromptWithResults();
                            }
                            long sec = countUp % 60;

                            String asText = "" + sec;
                            _textView.setText("Recorded: " + asText);
                        }
                    });


            if (_watch != null)
            _watch.start();
}

Is there a way to reset the chronometer so it does not pause but completely stop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-10-31 19:27:18

当我不久前使用计时器时,我只是在调用 start() 之前使用 setBase() 方法将基准设置为当前时间。根据您的具体需求,您可能需要在启动之前添加一些关于是否重置计时器的逻辑。

View.OnClickListener mStartButtonListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.start();
        }
    };

When I played with the chronometer awhile back I just used the setBase() method to set the base to the current time just before calling start(). Depending on your exact needs you may need to add some logic around whether to reset the chronometer or not before starting it.

View.OnClickListener mStartButtonListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.start();
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文