使倒数计时器从10秒变为1秒 [英] Make countdown timer go from 10sec to 1sec

查看:118
本文介绍了使倒数计时器从10秒变为1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CountDown计时器,该计时器从10000ms向下计数到0ms,以1秒为增量,使按钮在10秒后可单击。尽管计时器是准确的,并且执行了代码所说明的内容,但是我想更改秒的表达方式,但我不知道如何。

I have a CountDown timer that counts down from 10000ms to 0ms in increments of 1 second each to make a button clickable after 10 seconds. Although the timer is accurate and does what the code says, I would like to change the way the seconds are expressed but I don't know how.

java:

void startTimer() {
    cTimer = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            c.setText("Please wait " + millisUntilFinished/1000 + " seconds");
            thx.setText(millisUntilFinished/1000 + "");
            thx.setAlpha(.5f);
            thx.setClickable(false);

        }
        public void onFinish() {
        c.setText("done");
            thx.setText("ready");
            thx.setAlpha(1f);
            thx.setClickable(true);
        }
    };
    cTimer.start();
}

输出(每秒): 9,8, 7、6、5、4、3、2、1,(仍然是1),准备就绪

所需:(每秒): 10、9、8、7、6、5、4、3、2、1,准备就绪

谢谢,

B

编辑:

我在倒数中加了1, thx.setText((((millisUntilFinished / 1000)+ 1)+);

新输出: 10、9、8、7、6、5、4、3、2,(仍然是2),准备就绪

Closer ...,但不太完全。

Closer... but not quite.

推荐答案

这只是我的我几个月前使用CountDownTimer
时对CountDownTimer进行了调查,并且在我的应用程序中可以正常使用。

This is just my investigation on CountDownTimer when I used CountDownTimer few month before and its work fine in my application.

public void onTick(long millisUntilFinished)

此millisUntilFinished将给出剩余时间(以毫秒为单位),最后1000毫秒用于调用onFinish ()方法,因此onTick方法将被调用直到剩余时间超过(1000(对于OnFinish)+ 1000(对于计数器))毫秒,如果最后剩余的毫秒小于2000毫秒,它将跳过onTick(),并在计时器结束时直接调用onFinish()。有关更多详细信息,请参见处理程序方法。

This millisUntilFinished will give the remaining time in millisecond, and last 1000 millisecond is to call onFinish() method, So onTick method will get called until the remaining time is more than (1000(for OnFinish) + 1000(for counter)) milliseconds, if the last remaining millisecond is less than 2000millisecond it will skip the onTick() and it will directly call onFinish() when timer ends. For more details just see Handler method in this source.

因此,主要问题是当我们给定X(我们的情况为10000)毫秒,但是要开始时计数器大约需要50到150毫秒,因此,如果我们在总时间中加上该毫秒,我们将获得计数器直到结束,

So the main problem is when we give some X(our case 10000)milliseconds, but to start the counter it's taking some 50 to 150 milliseconds, So if we add that millisecond in our total time we will get the counter until end,

所以您可以尝试这样,没什么变化,只是我在您的总时间中增加了150毫秒。

So you can try like this, Nothing change just I added 150 milliseconds in your total time.

void startTimer() {
    cTimer = new CountDownTimer(10150, 1000) {
        public void onTick(long millisUntilFinished) {
            c.setText("Please wait " + millisUntilFinished/1000 + " seconds");
            thx.setText(millisUntilFinished/1000 + "");
            thx.setAlpha(.5f);
            thx.setClickable(false);

        }
        public void onFinish() {
        c.setText("done");
            thx.setText("ready");
            thx.setAlpha(1f);
            thx.setClickable(true);
        }
    };
    cTimer.start();
}

让我知道它是否有效,如果您问我为什么不使用处理程序,我可以说 CountDownTime 在内部使用处理程序。

Let me know if its works or not, If you ask me why you are not using Handler, I can say CountDownTime internally using Handler.

这篇关于使倒数计时器从10秒变为1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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