2012-04-10 160 views
0

我正在構建一個倒數計時器應用程序,我讓用戶按下一個按鈕來啓動計時器。當按下按鈕時,定時器倒計時(工作會話)。然後,用戶可以通過點擊相同的按鈕(重新標記爲「重置」)來等待定時器完成或重置定時器。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 ..

+0

控制是否超越了這種說法? vibrator.vibrate(V); – kosa 2012-04-10 14:32:29

+0

好像你的按鈕點擊不正常。你記錄了點擊事件嗎? – waqaslam 2012-04-10 14:33:46

回答

0

添加後

return; 

btStart.setText("Reset"); 
workCounter.start(); 

所以定時器不只是開始後取消。

+0

好呼叫人!!!!!!感謝一堆它工作!!!!!! – user772401 2012-04-10 14:56:30

相關問題