2014-10-09 164 views
0

我最近開始在我的第一個android應用程序上工作,我有點卡住了。我的應用程序使用CountDownTimer類,它看起來似乎是個錯誤 - CountDownTimer.cancel()不起作用。我知道如何通過修改CountDownTimer.java文件來解決這個問題,但是我沒有這個權限。我正在Android Studio工作。儘管這似乎是一個基本問題,但我無法在網上找到解釋,不是嗎?也許我沒有使用正確的條款?Android編輯系統文件

非常感謝您的幫助,我很抱歉如果我的問題有點無知哈哈。

回答

0

試試這個方法:類似

public class Main extends Activity implements OnClickListener 
    { 
     private static final String tag = "Main"; 
     private MalibuCountDownTimer countDownTimer; 
     private long timeElapsed; 
     private boolean timerHasStarted = false; 
     private Button startB; 
     private TextView text; 
     private TextView timeElapsedView; 

     private final long startTime = 50000; 
     private final long interval = 1000; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       startB = (Button) this.findViewById(R.id.button); 
       startB.setOnClickListener(this); 

       text = (TextView) this.findViewById(R.id.timer); 
       timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed); 
       countDownTimer = new MalibuCountDownTimer(startTime, interval); 
       text.setText(text.getText() + String.valueOf(startTime)); 
      } 

     @Override 
     public void onClick(View v) 
      { 
       if (!timerHasStarted) 
        { 
         countDownTimer.start(); 
         timerHasStarted = true; 
         startB.setText("Start"); 
        } 
       else 
        { 

         countDownTimer.cancel(); 
         timerHasStarted = false; 
         startB.setText("RESET"); 
        } 
      } 

     // CountDownTimer class 
     public class MalibuCountDownTimer extends CountDownTimer 
      { 

       public MalibuCountDownTimer(long startTime, long interval) 
        { 
         super(startTime, interval); 
        } 

       @Override 
       public void onFinish() 
        { 
         text.setText("Time's up!"); 
         timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime)); 
        } 

       @Override 
       public void onTick(long millisUntilFinished) 
        { 
         text.setText("Time remain:" + millisUntilFinished); 
         timeElapsed = startTime - millisUntilFinished; 
         timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed)); 
        } 
      } 
    } 

enter image description here

+0

我試過的東西,但它並不能幫助。看起來好像cancel()方法不會停止對onTick()的調用。無論如何,感謝您的幫助! – user2999193 2014-10-09 13:44:46