2015-11-18 44 views
0

我想用java在Android上製作倒數計時器。有一個計時器25分鐘學習計時器和5分鐘計時器。我有兩個計時器工作。但是,我不知道如何取消/清除計時器。如果我點擊我開始學習計時器和計時器,他們將同時運行,我不想這樣做。當另一個計時器打開時,我想要取消原始計時器。這是代碼。Java /安卓倒數計時器

btnStart.setOnClickListener(新OnClickListener(){

 public void onClick(View v){ 

btnStart.setEnabled(false); 
btnBreak.setEnabled(true); 

breakBoolean = false; 




      CountDownTimer timer; 
      long amountOfStudyTime = 1500000; //30 seconds (may cause problems) 
      long countDownInterval = 1000; //1 second 

      //Initialise the countdown timer 

      timer = new CountDownTimer(amountOfStudyTime, countDownInterval){ 

       public void onTick(long millisUntilFinished){ 

        if(studyBoolean = false) { 

         tViewTime.setText("CountDownTimer Canceled/stopped."); 
         cancel(); 
         breakBoolean = true; 

        }else{ 
         //display remaining seconds to user 

         tViewTime.setText(""+String.format("%d min, %d sec", 
           TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
           TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
           TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 

        } 
       } 

       public void onFinish(){ 
        //When countdown is finished do... 

        breakBoolean = true; 

        int currentScore = Integer.parseInt(editScore.getText().toString()); 

        int finalScore = currentScore + 5; 

        editScore.setText(Integer.toString(finalScore)); 


        tViewTime.setText("Done"); 

       } 

      }.start(); 
     } 
    }); 



    //Set a click listener for break button 
      btnBreak.setOnClickListener(new OnClickListener() { 

       public void onClick(View v){ 



        btnStart.setEnabled(true); 
        btnBreak.setEnabled(false); 


        studyBoolean = false; 



        CountDownTimer timer2; 
        long amountOfBreakTime = 300000; //30 seconds (may cause problems) 
        long countDownInterval = 1000; //1 second 

        //Initialise the countdown timer 

        timer2 = new CountDownTimer(amountOfBreakTime, countDownInterval){ 

         public void onTick(long millisUntilFinished){ 

          if(breakBoolean = false) { 


           cancel(); 
           studyBoolean = true; 

          }else{ 
           //display remaining seconds to user 

           tViewTime.setText(""+String.format("%d min, %d sec", 
             TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
             TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
             TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 

          } 
         } 

         public void onFinish(){ 
          //When countdown is finished do... 


          tViewTime.setText("Done"); 
          studyBoolean = true; 
         } 

        }.start(); 
       } 
      }); 

回答

0

創建您的活動二CountDownTimer對象並取消根據您的按鈕來選擇:

public class MainActivity extends Activity { 

    boolean breakIsRunning = false; 
    boolean startIsRunning = false; 

     Button btnStart,btnBreak; 

     CountDownTimer startTimer = new CountDownTimer(amountOfStudyTime, countDownInterval) 
     { 

      @Override 
      public void onFinish() { 
       //do something 
       startIsRunning = false; 
      } 

      @Override 
      public void onTick(long arg0) { 
       //do something 
       startIsRunning = true; 
      } 

     }; 



    CountDownTimer breakTimer = new CountDownTimer(amountOfBreakTime, countDownInterval) 
      { 

       @Override 
       public void onFinish() { 
        //do something 
        breakIsRunning = false; 
       } 

       @Override 
       public void onTick(long arg0) { 
        //do something 
        breakIsRunning = true; 
       } 

      }; 


//->OnCreate() - >Buttons code 

    btnStart.setOnClickListener(new OnClickListener().. { // your listener code here 
      if(breakIsRunning) 
      breakTimer.cancel(); 
      startTimer.start(); 
    } 

    btnBreak.setOnClickListener(new OnClickListener().. { // 
      if(startIsRunning) 
       startTimer.cancel(); 
      breakTimer.start(); 
    } 

}