【问题标题】:Custom countdown timer Android Studio自定义倒数计时器 Android Studio
【发布时间】:2019-02-13 23:15:35
【问题描述】:

我想在 Android Studio 中创建一个自定义倒数计时器,但我希望它在 2 个计数之间具有更长的持续时间/间隔,而不是正常的倒数计时器。请帮帮我。

【问题讨论】:

    标签: java android android-studio countdowntimer


    【解决方案1】:

    通过设置 countDownInterval 参数,您可以更改两个刻度之间的间隔:

    private CountDownTimer mCountDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
    
        public void onTick(long millisUntilFinished) {
            // do sth here...
        }
    
        public void onFinish() {
            // do sth here...
        }
    };
    

    例如,将其设置为 2000,计时器每 2 秒计时一次。 启动或停止计时器:

    mCountDownTimer.start();
    mCountDownTimer.cancel();
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

              new CountDownTimer(30000, 1000) {//initial interval is one second
                  private int i = 0;
      
                  public void onTick(long millisUntilFinished) {
                      i++;
                      if (i % 2 == 0) {//if you want a longer interval to do something
                          //practical interval is now two seconds, change as you want.
                          ......
                      }
                  }
      
                  public void onFinish() {
                          .....
                  }
              }.start();
      

      【讨论】:

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