【问题标题】:When I click cancel button I want to stop the countdown当我单击取消按钮时,我想停止倒计时
【发布时间】:2017-04-24 16:09:12
【问题描述】:

当我单击开始按钮时,倒数计时器正常工作,但当我单击取消按钮时,我的应用程序会关闭。

private View.OnClickListener btnOnClickListener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {  // checking button is clicked
           switch(v.getId()){
               case R.id.b_pick:
                   start();             //start button
                   break;
               case R.id.b_cancel:
                   cancel();              //cancel button
                   break;

           }

            }
    };

以下代码是倒计时方法。单击取消按钮后,我想停止倒计时时间并将result3标签的文本设置为“取消”。

  private void count() {
            countDownTimer : new CountDownTimer(difference,1000) {      // setting countdown time as date difference in miliseconds
                @Override
                public void onTick(long millisUntilFinished) {

                    result3.setText((millisUntilFinished/1000)+" seconds");

                }

                @Override
                public void onFinish() {
                    result3.setText("finished"); // on finish set text to finished

                }
            }.start();  // starting the countdown timer
        }


    // cancel countdown
        public void cancel(){
            //result2.setText("canceled");
    countDownTimer.cancel();
    if(countDownTimer!=null) {
        countDownTimer.cancel();
        result3.setText("canceled");
        countDownTimer = null;
}
    }

【问题讨论】:

  • 您正在添加此行 (countDownTimer.cancel()) 以取消它两次。删除 if not null 之前的那个。看看你的 logcat 它说了什么?

标签: android


【解决方案1】:

提示.. 我建议使用一个按钮,该按钮将根据定时器是打开还是关闭来控制定时器。您可以设置一个全局标志 (bool) 以了解计时器是打开还是关闭。我不确定我的问题是否正确。

【讨论】:

    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多