Android 我怎样才能让我的CoontDowntimer停止滴答作响?
我正在用安卓系统制作一个简单的计时器,使用Android 我怎样才能让我的CoontDowntimer停止滴答作响?,android,timer,countdowntimer,numberpicker,Android,Timer,Countdowntimer,Numberpicker,我正在用安卓系统制作一个简单的计时器,使用倒数计时器和一些NumberPickers。现在,当用户点击“我的”按钮,计时器没有运行时,它将启动,当用户点击同一按钮,计时器正在倒计时时,它应该停止。我的问题是它没有停止。我调用了CountDownTimer.cancel(),但这似乎没有任何作用,onTick()中的代码仍在继续执行,这意味着NumberPickers仍在递减,就好像计时器仍在运行一样 我不确定这是否是我的代码,或者我只是不正确地使用了倒计时。因此,当我调用Cancel()方法时,
倒数计时器和一些NumberPickers
。现在,当用户点击“我的”按钮,计时器没有运行时,它将启动,当用户点击同一按钮,计时器正在倒计时时,它应该停止。我的问题是它没有停止。我调用了CountDownTimer.cancel()
,但这似乎没有任何作用,onTick()
中的代码仍在继续执行,这意味着NumberPickers
仍在递减,就好像计时器仍在运行一样
我不确定这是否是我的代码,或者我只是不正确地使用了倒计时。因此,当我调用Cancel()
方法时,onTick()
事件是否应该停止触发?他们似乎不是这样我怎么能这样做
我将把我的一些代码放在这里:
我的按钮代码:
@Override
public void onClick(View v) {
//Calculate total time from NumberPickers in seconds
long startTime = (numberPicker1.getValue() * 60) * 1000 + numberPicker2.getValue() * 1000;
Log.i(TAG, "Start time: " + startTime + "");
//Create CountDownTimer with values from NumberPickers
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1000)); //should be removed
if(!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
//Disable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(false);
numberPicker2.setEnabled(false);
startButton.setText(R.string.stop);
} else {
countDownTimer.cancel();
//countDownTimer = countDownTimer.pause();
timerHasStarted = false;
//Re-enable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(true);
numberPicker2.setEnabled(true);
startButton.setText(R.string.restart);
}
}
我的计时器的onTick()代码:
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1000);
//Decrement the NumberPickers after each second
if(numberPicker2.getValue() > 0) {
//When seconds left is greater than 0 decrement seconds
changeValueByOne(numberPicker2, false);
}
else if(numberPicker1.getValue() > 0 && numberPicker2.getValue() <= 0) {
//Else, if seconds <= 0 and minutes > 0 decrement minutes and reset seconds to 59
changeValueByOne(numberPicker1, false);
numberPicker2.setValue(59);
}
else if(numberPicker1.getValue() <= 0 && numberPicker2.getValue() <= 0){
//Else, if both minutes and seconds <= 0 revert back to 0 and finish
//This should never really happen, but just in case
numberPicker1.setValue(0);
numberPicker2.setValue(0);
Log.i(TAG, "There is a tick when both are 0");
}
this.millisUntilFinished = millisUntilFinished;
}
@覆盖
公共void onTick(长毫秒未完成){
text.setText(“+millisuntiltfinished/1000);
//在每秒钟后减少NumberPickers
如果(numberPicker2.getValue()>0){
//当剩余秒数大于0时,递减秒数
changeValueByOne(数字picker2,false);
}
否则如果(numberPicker1.getValue()>0&&numberPicker2.getValue()问题:
在onClick()
中,您正在为每次单击创建一个新的MyCountDownTimer
对象,而不检查TimerHassStart
的值
countDownTimer = new MyCountDownTimer(startTime, interval);
然后对这个新对象调用cancel(),因此倒计时计时器不会停止并继续
解决方案:
仅当timerHasStarted
的值为false(在if块中)时,才创建MyCountDownTimer
对象
啊,当然!真不敢相信我竟然没有注意到。非常感谢!
@Override
public void onClick(View v) {
//Calculate total time from NumberPickers in seconds
long startTime = (numberPicker1.getValue() * 60) * 1000 + numberPicker2.getValue() * 1000;
Log.i(TAG, "Start time: " + startTime + "");
if(!timerHasStarted) {
//Create CountDownTimer with values from NumberPickers
countDownTimer = new MyCountDownTimer(startTime, interval);
countDownTimer.start();
timerHasStarted = true;
//Disable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(false);
numberPicker2.setEnabled(false);
startButton.setText(R.string.stop);
} else {
countDownTimer.cancel();
//countDownTimer = countDownTimer.pause();
timerHasStarted = false;
//Re-enable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(true);
numberPicker2.setEnabled(true);
startButton.setText(R.string.restart);
}
}