使倒数计时器从 10 秒变为 1 秒

Make countdown timer go from 10sec to 1sec

我有一个 CountDown 计时器,它从 10000 毫秒倒计时到 0 毫秒,每次递增 1 秒,使按钮在 10 秒后可点击。虽然计时器很准确并且按照代码所说的进行操作,但我想更改秒数的表示方式,但我不知道如何更改。

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, (still 1), ready

期望:(每秒):10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ready

谢谢,

B

编辑:

我倒计时加了1,thx.setText(((millisUntilFinished/1000) + 1) + "");

新输出:10, 9, 8, 7, 6, 5, 4 ,3 , 2, (Still 2), ready

更接近...但不完全是。

试试这个计数器:

   public void CountDown() {
    final TextView textic = (TextView) findViewById(R.id.tvConuter);

    CountDownTimer Count = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            long str = millisUntilFinished / 1000;
            String TimeFinished = String.valueOf(str);
            textic.setText(TimeFinished);
        }
        public void onFinish() {
            textic.setText("STOP");

        }
    };
    Count.start();
}

这对我来说非常有效。

我建议使用 Timer()Handler() class。

int tick = 0;
    Handler handler = new Handler();
    Runnable r = new Runnable() {void run(){
if (i < 10) {
handler.postDelayed()}
else {
//finished
tick = 0;
}}};

开始:

handler.post(r);

new CountDownTimer(10000, 1000) {

public void onTick(long millisUntilFinished) {
    mTextField.setText("seconds: " + millisUntilFinished / 1000);
   //here you can have your logic to set text to edittext
}

public void onFinish() {
    mTextField.setText("done!");
}

}.start();

这只是我在使用CountDownTimer时对CountDownTimer的调查 几个月前,它在我的应用程序中运行良好。

public void onTick(long millisUntilFinished)

这个millisUntilFinished会以毫秒为单位给出剩余时间,最后1000毫秒是调用onFinish()方法,所以onTick方法会一直调用到剩余时间大于(1000(for OnFinish) + 1000(for counter)) 毫秒,如果最后剩余的毫秒数小于 2000 毫秒,它将跳过 onTick() 并在定时器结束时直接调用 onFinish()。有关详细信息,请参阅此来源中的 Handler 方法。

所以主要的问题是当我们给了一些 X(我们的例子是 10000)毫秒,但是启动计数器需要大约 50 到 150 毫秒,所以如果我们在总时间中加上那个毫秒,我们将得到计数器直到结束,

所以你可以这样尝试,我在你的总时间中增加了 150 毫秒,没有任何改变。

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();
}

让我知道它是否有效,如果你问我为什么不使用 Handler,我可以说 CountDownTime 在内部使用 Handler。

private void countDownTimer(int seconds) {

    countDownTimer = new CountDownTimer(seconds * 1000L, 1000 - 500) {

        public void onTick(long millisUntilFinished) {

            //Convert to seconds
            int secondsUnillFinished = (int) (millisUntilFinished / 1000);

            //Set text to seconds
            textView.setText(String.valueOf(secondsUnillFinished));

            //At zero, do something
            if(secondsUnillFinished == 0) {
                countDownTimer.cancel();
                textView.setVisibility(View.INVISIBLE);
            }
        }

        @SuppressLint("SetTextI18n")
        public void onFinish() {
           //Dont put anything here
        }

    }.start();
}