使用 onchronometerticklistener 的 if 语句

发布于 2025-01-04 14:28:40 字数 648 浏览 0 评论 0原文

我正在尝试使用计时器创建一些基于时间的事件。下面的代码在计时器启动 5 秒后成功显示吐司,但我需要在 5/10/20 秒时显示吐司。

我尝试过使用 if (elapsedTime == 5000 ||elapsedTime == 10000 ) 但当我使用 == 时没有显示 toast。

有什么想法吗?

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { // listens to journey timer to initiate time based events

    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > 5000)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();

        }

    }
};  

I'm trying to create some time based events using a chronometer. The code below is successfully displaying a toast after 5 seconds of chronometer being started however I need the toast to be displayed at 5/10/20 seconds.

I've tried using if (elapsedTime == 5000 ||elapsedTime == 10000 ) but no toast displays when I use ==.

Any ideas?

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { // listens to journey timer to initiate time based events

    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > 5000)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();

        }

    }
};  

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

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

发布评论

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

评论(1

ˉ厌 2025-01-11 14:28:40

尝试这样的操作(如果您想此后每 10 秒保持一次):

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { 
    // listens to journey timer to initiate time based events
    int currentNeededTime = 5000;
    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > currentNeededTime)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();
            currentNeededTime += currentNeededTime == 5000 ? 5000 : 10000;
        }
    }
};

Try something like this (if you want to keep it going every 10 seconds thereafter):

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { 
    // listens to journey timer to initiate time based events
    int currentNeededTime = 5000;
    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > currentNeededTime)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();
            currentNeededTime += currentNeededTime == 5000 ? 5000 : 10000;
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文