我正在构建倒计时器应用程序,我让用户按下按钮启动计时器。按下按钮时,计时器倒计时(工作会话)。然后,用户可以通过单击相同的按钮(重新标记为“重置”)等待计时器完成或重置计时器。
如果他们等待计时器完成,则启动另一个计时器(短时间间隔 - 即中断会话)。此时,如果按下按钮(重新标记为“结束中断”),它将取消中断计时器并启动另一个工作会话。
出于某种原因,当我点击我的按钮时,它将无法启动计时器。在我目前的代码设置中。我测试了计时器和按钮,我知道它们有效。
由于某些原因,我的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(关闭){...}语句中启动...当我将其更改为其他配置时,它将不会取消我当前运行的CountDownTimer ..
答案 0 :(得分:0)
添加
return;
之后
btStart.setText("Reset");
workCounter.start();
所以在开始后不会取消计时器。