通过按钮单击向倒数计时器添加五秒钟

时间:2014-05-26 14:55:34

标签: android timer countdowntimer

您好我正在每个问题上实施一个带计时器的问答游戏。 倒数计时器当前为十秒长,从onCreate方法开始,按预期倒计时。

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

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

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



    }
}

1 个答案:

答案 0 :(得分:0)

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