我正在构建一个倒数计时器应用程序,我让用户按下一个按钮来启动计时器。当按下按钮时,计时器倒计时(工作会话)。然后,用户可以等待计时器完成或通过单击相同的按钮(重新标记为“重置”)重置计时器。
如果他们等待计时器完成,则会启动另一个计时器(短时间间隔 - 也就是中断会话)。此时,如果他们按下按钮(重新标记为“结束休息”),它将取消休息计时器并开始另一个工作会话。
出于某种原因,当我单击按钮时,它不会启动计时器。在我当前的代码设置中。我已经测试了计时器和按钮,我知道它们可以工作。
出于某种原因,我的 onClick 方法没有启动计时器,有什么帮助吗?我必须对我的 CountDownTimer 课程做些什么吗?
public class SimplyPomodoroActivity extends Activity implements OnClickListener {
TextView tvTimer; // used to update timer...
Button btStart; //main button
Vibrator vibrator; // vibrate when button is pressed..
boolean off = true;
boolean working = false;
long longBreak = 8000; // 900000;
long shortBreak = 6000; // 300000;
long workTime = 10000; // 1500000;
long v = 100; // vibration sequence
int pomoCount = 1; // keep track of the number of Pomodoros...
// PomoTimer pomoBreak = new PomoTimer(startTime, interval);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialiaze(); //connect xml to java code and setup listener
}
private void initialiaze() {
tvTimer = (TextView) findViewById(R.id.tvTimer);
btStart = (Button) findViewById(R.id.btStart);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
btStart.setOnClickListener(this); // register listener
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vibrator.vibrate(v);
//Do stuff
if(off){ //Turn on
//change text
//start work timer --> work timer will go to break automatically
off = false;
working = true;
btStart.setText("Reset");
workCounter.start();
}
if(working){
//turn off
btStart.setText("Start");
workCounter.cancel();
working = false;
off = true;
}else if(!working && !off){
//end break
shortBreakCounter.cancel();
btStart.setText("Reset");
workCounter.start();
}
}
CountDownTimer workCounter = new CountDownTimer(workTime, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
tvTimer.setText("0:00");
working = false;
pomoIncrement();
btStart.setText("End Break");
shortBreakCounter.start();
}
};
CountDownTimer shortBreakCounter = new CountDownTimer(shortBreak, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
working = true;
pomoIncrement();
btStart.setText("Reset");
workCounter.start();
}
};
CountDownTimer longBreakCounter = new CountDownTimer(longBreak, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
pomoIncrement();
}
};
private void pomoIncrement() {
// increment by one, reset at 8
pomoCount += (pomoCount > 8) ? -pomoCount : 1;
}
private void displayRemainingTime(long millisUntilFinished) {
// TODO Auto-generated method stub
int sec = (int) (millisUntilFinished / 1000) % 60;
int min = (int) ((millisUntilFinished / 1000) / 60);
tvTimer.setText("" + min + ":" + sec);
}
}
我的倒数计时器不会在我的 if(off){...} 语句中启动...当我将其更改为其他配置时,它不会取消我当前正在运行的 CountDownTimer..