1

在我的应用程序中,我应该使用CountDownTimer,为此我想使用这个库:CountdownView
我想在持续 5 秒的时候给我看Toast
例如:我想显示 17s countTimer,当这个计时器接收到 5s 时显示toast“仅 5s”。
我使用此代码,但给我看 5s 。

long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(5000, new CountdownView.OnCountdownIntervalListener() {
    @Override
    public void onInterval(CountdownView cv, long remainTime) {
        Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
    }
});

我只想持续 5s 而不是 5s。

我该怎么办?

4

2 回答 2

2

通过修改您的代码:

boolean toastShown = false;
long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
    @Override
    public void onInterval(CountdownView cv, long remainTime) {
        if(remainTime < 5000 && !toastShown) {
            toastShown = true;
            Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
        }
    }
});

您也可以尝试此代码:

CountDownTimer countDownTimer = new CountDownTimer(17000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        Toast.makeText(HomeActivity.this, "running" + millisUntilFinished, Toast.LENGTH_SHORT).show();
        if (millisUntilFinished < 5000) {
            Toast.makeText(HomeActivity.this, "5 sec left", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFinish() {
        Toast.makeText(HomeActivity.this, "Countdown Timer Finished", Toast.LENGTH_SHORT).show();
    }
};
countDownTimer.start();
于 2017-06-09T08:11:32.570 回答
0

我认为您应该检查还剩多少秒。我重写了你的代码:

long time1 = 17L * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
    @Override
    public void onInterval(CountdownView cv, long remainTime) {
        long remainTimeInSeconds = remainTime / 1000;
        if (remainTimeInSeconds  == 5) {
            Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
        }
    }
});
于 2017-06-09T08:01:00.220 回答