4

我正在制作一个国际象棋时钟,但在其中我需要延迟(就像它在计数前等待 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 并使应用程序没有响应,因为它现在正在处理处理程序。任何帮助将不胜感激!提前致谢!

4

3 回答 3

4

我认为您不应该将 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);
            }

        }
    });
}
于 2017-02-08T03:53:33.533 回答
0

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

    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);
        }
    });
于 2017-02-08T03:42:15.580 回答
0

您可以避免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()
}

但是第一个滴答声将没有您的计算

于 2020-11-15T18:25:29.573 回答