【问题标题】:Adding five seconds to countdown timer with button click通过单击按钮向倒数计时器添加五秒
【发布时间】:2014-07-15 09:19:49
【问题描述】:

您好,我正在实施一个问答游戏,每个问题都有一个计时器。 Countdown 计时器目前为十秒长,在 onCreate 方法中启动并按预期倒计时。

但是,当用户单击按钮时,我正在尝试实现计时器更新额外的 5 秒。我尝试的任何东西似乎都不起作用。我不确定是否需要创建一个新计时器并仅更新文本视图。

我当前的代码如下所示。非常感谢您的帮助。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 


...........

  }       

} catch (InterruptedException e) {

    e.printStackTrace();
} catch (ExecutionException e) {

    e.printStackTrace();
}
       startTimer(10000); 
    }

private void startTimer(long remaningMillis){

    new CountDownTimer(10000, 1000){

        public void onTick(long millisUntilFinished) {
             time.setText("Timer: " + millisUntilFinished / 1000);
         }

         public void onFinish() {
             time.setText("Timer: 0");
             alert("Oh, no! You are out of Time! You Lost a Life");
             --mLives;
             lives.setText(String.valueOf(mLives));
             saveData();
         }
      }.start();
}



public void onTimeBoostButtonClicked(View arg0) {
    Log.d(TAG, "Time Boost button clicked.");
    if (mTimeBoosts == 0) alert("Oh, no! You are out of Time Boosts! Try buying some!");
    else {
         --mTimeBoosts;
        TextView NumTimeBooststextView = (TextView) this.findViewById(R.id.NumTimeBooststextView);
        NumTimeBooststextView.setText(String.valueOf(mTimeBoosts));
        saveData();

       //TODO Add 5 seconds to timer



    }
}

【问题讨论】:

    标签: android timer countdowntimer


    【解决方案1】:

    根据此 SO 帖子here,您无法更改计划计时器的倒计时,您可以取消它并开始一个新计时器。请检查链接中接受的答案。

    【讨论】:

    • 我怎么没看到这个?无论如何,我会接受这个作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多