CountDown计时器的延迟 - Android

时间:2017-02-08 02:41:13

标签: java android timer countdowntimer android-timer

我正在制作国际象棋时钟,但在其中我需要一个延迟(就像它在计数前等待10秒)。我使用了Handler,但是如果在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);

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

3 个答案:

答案 0 :(得分:3)

我认为你不应该将CountdownTimer放入Handler。你可以创建2个处理程序。这是一个例子。

// using map function
var m = gillFamily.map(function(a) {
  // check if 20 is less than age of the person
  // if so then delete the age key
  20 < a.age && delete a.age;
  return a // return the object
});
console.log(m);

答案 1 :(得分: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);
        }
    });

答案 2 :(得分: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()
}

但是第一个刻度不会进行计算