计时器在 Android 中不会停止

发布于 2024-11-02 15:19:05 字数 862 浏览 4 评论 0原文

在我的应用程序中,我想显示一个停止计时器手表。当我通过谷歌搜索时,我在开发者网站上发现了一个名为 Chronometer 的选项。它看起来就像一个停止计时器的手表。

当我单击开始按钮时,我希望计时器开始,当我单击暂停按钮时,计时器必须暂停,一旦我单击开始,计时器必须从停止时开始。

但在这个计时器中,它从 0 开始,当我在 1 分 10 秒处单击暂停时,它就会暂停。当我在 5 分钟后再次单击开始时,计时器从 6 分 10 秒开始计数,即使在暂停时计时器仍在运行,如何停止并在停止时恢复。

以下是我的天文台代码

Start = (Button)findViewById(R.id.widget306);
        Start.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                chronometer.start();                
            }
        });

        Stop = (Button)findViewById(R.id.widget307);
        Stop.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                  chronometer.stop();
            }
        });
    }

In my app I want to show a stop timer watch. When I searched through Google I found an option called Chronometer in the developers site. It look's just like a stop timer watch.

When I click the start button I want the timer to get started and when I click the pause button the timer must be paused when once I click the start it must start from the time it's been stopped.

But in this chronometer it get's started from 0 and when I click pause at 1min 10 sec it get paused. When I click once again start after 5min, the timer start the count from 6min 10sec, even at pause the timer is running, how to stop this and get resumed at the time it is been stopped.

Following is my code for chronometer

Start = (Button)findViewById(R.id.widget306);
        Start.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                chronometer.start();                
            }
        });

        Stop = (Button)findViewById(R.id.widget307);
        Stop.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                  chronometer.stop();
            }
        });
    }

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

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

发布评论

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

评论(4

暮色兮凉城 2024-11-09 15:19:05

抱歉,但我自己找到了一个解决方案,它看起来更简单:

  1. 在您的活动中,声明一个成员变量(例如lastPause)来记录您上次暂停的时间:

    private long lastPause;

  2. 启动计时器(假设计时器的变量是 crono):

    crono.setBase(SystemClock.elapsedRealtime());

    crono.start();

  3. 每次使用时暂停计时器:

    lastPause = SystemClock.elapsedRealtime();

    crono.stop();

  4. 每次使用以下命令恢复计时器:

    crono.setBase(crono.getBase() + SystemClock.elapsedRealtime() - lastPause);

    crono.start();

我还没有精确测试精度,因为我的眼睛似乎没问题:]

Sorry, but I've found a solution myself and it seems much simpler:

  1. In your activity, declare a member variable(e.g. lastPause) to record the time of your last pause:

    private long lastPause;

  2. Start your timer (assume the variable of your timer is crono):

    crono.setBase(SystemClock.elapsedRealtime());

    crono.start();

  3. Pause your timer each time using:

    lastPause = SystemClock.elapsedRealtime();

    crono.stop();

  4. Resume your timer each time using:

    crono.setBase(crono.getBase() + SystemClock.elapsedRealtime() - lastPause);

    crono.start();

I've not tested the precision precisely yet as it seems to be ok by my eyes :]

红焚 2024-11-09 15:19:05

来自计时器 doc

停止计数。这不影响
从 setBase(long) 设置的基数,
只是视图显示。

我现在没有任何 Android 开发环境,所以无法对其进行更多调查:(

编辑:
这是代码 你的代码基于?似乎在停止时使用 setBase 来设置当前时间

From the Chronometer doc

Stop counting up. This does not affect
the base as set from setBase(long),
just the view display.

I dont have any android dev environment right now, so cant investigate on it more :(

Edit:
is it the code you have based your code on ? It seems to use setBase to the current time on stop

岁月静好 2024-11-09 15:19:05

我遇到了同样的问题,并使用以下代码修复了它:

chronometerInstance.setBase(SystemClock.elapsedRealtime());
chronometerInstance.stop();

这使我的计时器停止计数到 00:00。

I faced the same issue and I fixed it by using the following code:

chronometerInstance.setBase(SystemClock.elapsedRealtime());
chronometerInstance.stop();

This is stopped my chronometer counting to 00:00.

杀お生予夺 2024-11-09 15:19:05

下面的代码创造了奇迹:

final long lastPause = SystemClock.elapsedRealtime();
mChronometer.stop();

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity)
    .setCancelable(false)
    .setMessage("Stop chronometer while AlertDialog is asking something.")
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            mChronometer.setBase(mChronometer.getBase() 
                + SystemClock.elapsedRealtime() 
                - lastPause);
            mChronometer.start();
        }
    });
alertDialogBuilder.create().show();

这个片段只是关于停止计时器并显示一个 AlertDialog。当对话框关闭时,计时器将从最后的位置开始计时。

The following works wonders:

final long lastPause = SystemClock.elapsedRealtime();
mChronometer.stop();

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity)
    .setCancelable(false)
    .setMessage("Stop chronometer while AlertDialog is asking something.")
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            mChronometer.setBase(mChronometer.getBase() 
                + SystemClock.elapsedRealtime() 
                - lastPause);
            mChronometer.start();
        }
    });
alertDialogBuilder.create().show();

This snippet is just about stopping the chronometer and showing an AlertDialog. When the Dialog is closed the chronometer will start from it's last position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文