倒数计时器延迟 - Android

Delay For CountDown Timer - Android

我正在制作一个国际象棋时钟,但我需要在其中延迟(比如在计数前等待 10 秒)。我为此使用了处理程序,但如果在 10 秒内单击按钮,则什么也不会发生。请帮忙!谢谢! 我的代码:

    mHandler.postDelayed(new Runnable() {
                        public void run() {
                            // count down timer start
                            timer2 = new CountDownTimer(totalSeconds, Integer.parseInt(delay.getText().toString())) {
                                public void onTick(long millisUntilFinished) {
                                    secondsTimer = (int) (millisUntilFinished / 1000) % 60;
                                    minutesTimer = (int) ((millisUntilFinished / (1000 * 60)) % 60);
                                    hoursTimer = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
                                    person2.setText(hoursTimer + ":" + minutesTimer + ":" + secondsTimer);
                                }

                                public void onFinish() {
                                    person2.setText("Time Up!");
                                    person2.setBackgroundColor(Color.RED);
                                    mp.start();
                                }
                            }.start();
                        }
                    }, finalDelay);

我想要延迟,但我不想锁定 UI 并使应用程序停止响应,因为它现在正在使用处理程序。任何帮助将不胜感激!提前致谢!

如果您想立即启动定时器,

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Handler().post(new Runnable() {
                public void run() {
                    // count down timer start
                    new CountDownTimer(100000, 1000) {
                        public void onTick(long millisUntilFinished) {
                            button.setText(String.valueOf(millisUntilFinished));
                        }

                        public void onFinish() {
                            button.setText("Time Up!");
                        }
                    }.start();
                }
            });
        }
    });

而如果你想在一定时间后执行,那么

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    // count down timer start
                    new CountDownTimer(100000, 1000) {
                        public void onTick(long millisUntilFinished) {
                            button.setText(String.valueOf(millisUntilFinished));
                        }

                        public void onFinish() {
                            button.setText("Time Up!");
                        }
                    }.start();
                }
            }, 1000);
        }
    });

我认为你不应该把 CountdownTimer 放到 Handler 中。您可以改为创建 2 个处理程序。这是一个例子。

private void startHandlerAndWait10Seconds(){
    Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {

        public void run() {
            // Start Countdown timer after wait for 10 seconds
            startCountDown();

        }
    }, 10000);
}

private void startCountDown {
    final Handler handler2 = new Handler();
    handler2.post(new Runnable() {
        int seconds = 60;

        public void run() {
            seconds--;
            mhello.setText("" + seconds);
            if (seconds < 0) {
                // DO SOMETHING WHEN TIMES UP
                stopTimer = true;
            }
            if(stopTimer == false) {
                handler2.postDelayed(this, 1000);
            }

        }
    });
}

您可以避免使用Handler立即启动计时器

   private fun start10SecondsDelayTimer() {
    var delayCount = 10
    val timer = object : CountDownTimer(1_000_000L, 1000L) {
        override fun onTick(millisUntilFinished: Long) {
            if (delayCount > 0) {
                delayCount--
            } else {
                //your calculations
            }
        }

        override fun onFinish() {
            //some calculations
        }
    }
    timer.start()
}

但第一个报价将没有您的计算