【问题标题】:Delay For CountDown Timer - Android倒数计时器的延迟 - Android
【发布时间】:2017-02-08 02:41:12
【问题描述】:

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

【问题讨论】:

    标签: java android timer countdowntimer android-timer


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这个答案看起来很有帮助,但你能解释一下startHandlerAfterWaited10Seconds() 方法中发生的事情吗?尽管发现它很有用,我还是赞成你的答案!
    • 嗨,Kharbanda,你提到的方法就像倒数计时器。我建议您使用 Handler 而不是 CountDown 计时器类的原因是: - Handler 应该比 CountDown 计时器类具有更好的性能。你可以在这里找到有用的参考stackoverflow.com/questions/35497844/… 另外,CounDown 计时器实际上在其核心中实现了 Handler。
    • 好的,谢谢!我发现你的解释和网站一样有用!非常感谢。但只是为了确定,倒数计时器是startHandlerAndWait10Seconds()的方法对吗?
    • 嘿,Kharbanda,startHandlerAndWait10Seconds() 用于在启动倒数计时器之前等待 10 秒,startHandlerAfterWaited10Seconds() 是倒数计时器。我将重命名以使其清楚。
    【解决方案2】:

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

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

    【讨论】:

    • 我确实想要延迟,但我只是不想锁定 UI 并使应用程序无响应。
    【解决方案3】:

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

    但第一个滴答声无需您计算

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多