onClick方法不会启动我的CountDownTimer - 为什么?
问题描述:
我正在构建一个倒数计时器应用程序,我让用户按下一个按钮来启动计时器。当按下按钮时,定时器倒计时(工作会话)。然后,用户可以通过点击相同的按钮(重新标记为“重置”)来等待定时器完成或重置定时器。onClick方法不会启动我的CountDownTimer - 为什么?
如果他们等待计时器结束,另一个计时器启动(短时间间隔 - 又名休息时间)。此时,如果他们按下按钮(重新标记为“结束休息”),它将取消休息计时器并启动另一个工作会话。
由于某些原因,当我点击我的按钮时,它不会启动计时器。在我目前的代码设置。我测试了计时器和按钮,我知道他们工作。
出于某种原因,我的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);
}
}
我的倒计时器将不是我的,如果(关闭){...}语句开始......当我周围改成了其他配置也不会取消我的当前运行CountDownTimer ..
答
添加后
return;
btStart.setText("Reset");
workCounter.start();
所以定时器不只是开始后取消。
+0
好呼叫人!!!!!!感谢一堆它工作!!!!!! – user772401 2012-04-10 14:56:30
控制是否超越了这种说法? vibrator.vibrate(V); – kosa 2012-04-10 14:32:29
好像你的按钮点击不正常。你记录了点击事件吗? – waqaslam 2012-04-10 14:33:46