2012-04-10 8 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

+0

があなたのボタンのように思われて、クリックが正しく機能していません。クリックイベントを記録しましたか? – waqaslam

答えて

0

ので、タイマーがちょうど開始後にキャンセルされていない

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

return; 

を追加します。

+0

Good Call man !!!!!!ありがとう、それは働いた束!!!!!! – user772401

関連する問題