您好我正在每个问题上实施一个带计时器的问答游戏。 倒数计时器当前为十秒长,从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
}
}