2014-02-09 48 views
0

我寫了一個定時器代碼。清除功能不會清除時間值(當我按下清楚只有文字的變化,但時間價值繼續形式的停止點),這裏是代碼,有什麼建議,以激活清除按鈕?:android開發人員 - 定時器應用程序

public class MainActivity extends Activity { 

    private Button startButton;  
    private Button stopButton; 
    private Button clearButton; 
    private TextView timeValue;  
    private long timeStart = 0L; 
    private Handler timeHandler = new Handler(); 
    long timeInMilisec = 0L; 
    long timeMemo = 0L; 
    long timeUpdate = 0L; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     timeValue = (TextView) findViewById(R.id.timeValue);   
     startButton = (Button) findViewById(R.id.startButton);  
     startButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

       timeStart = SystemClock.uptimeMillis();  
       timeHandler.postDelayed(updateTimerThread, 0); 

      } 

     }); 

     stopButton = (Button) findViewById(R.id.stopButton);   
     stopButton.setOnClickListener(new View.OnClickListener() {  

      public void onClick(View view) {   
       timeMemo += timeInMilisec;  
       timeHandler.removeCallbacks(updateTimerThread); 

      } 

     }); 
    clearButton = (Button) findViewById(R.id.clearButton); 
    clearButton.setOnClickListener(new View.OnClickListener() {     
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.clearButton: 

       timeValue.setText("00:00:00"); 
       int secs = 0;  
       int mins = 0;  
       secs = 0; 
       int milliseconds = 0; 
       timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
       timeUpdate = timeMemo + timeInMilisec;  
       timeValue.setText("" + mins + ":"  
         + String.format("%02d", secs) + ":"  
         + String.format("%03d", milliseconds)); 

       timeHandler.removeCallbacksAndMessages(updateTimerThread); 
       timeValue.setText("00:00:00"); 

       break;     
      }     
     } 
    }); 

    } 

    private Runnable updateTimerThread = new Runnable() {  

     public void run() { 

      timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
      timeUpdate = timeMemo + timeInMilisec;  
      int secs = (int) (timeUpdate/1000);  
      int mins = secs/60;  
      secs = secs % 60;  
      int milliseconds = (int) (timeUpdate % 1000);  
      timeValue.setText("" + mins + ":"  
        + String.format("%02d", secs) + ":"  
        + String.format("%03d", milliseconds));  
      timeHandler.postDelayed(this, 0); 

     } 

    };  


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

回答

0

在ClearButton onClick中,您不重置「timerstart」和「timeMemo」變量的值。這就是爲什麼只有文本正在更新但計時器沒有被重置。

0

您應該重置timeMemo變量。

clearButton.setOnClickListener(new View.OnClickListener() {     
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.clearButton: 

      // Clear the timestart and timeMemo 
      timeMemo = 0L; 

      timeValue.setText("00:00:00"); 
      int secs = 0;  
      int mins = 0;  
      secs = 0; 
      int milliseconds = 0; 
      timeInMilisec = SystemClock.uptimeMillis() - timeStart;  
      timeUpdate = timeMemo + timeInMilisec;  
      timeValue.setText("" + mins + ":"  
        + String.format("%02d", secs) + ":"  
        + String.format("%03d", milliseconds)); 

      timeHandler.removeCallbacksAndMessages(updateTimerThread); 
      timeValue.setText("00:00:00"); 
      timeHandler.removeCallbacks(updateTimerThread); 

      break;   
     }     
    } 
}); 
+0

謝謝的人 - 它的工作:-) –

+0

不客氣:)順便說一句,你應該標記答案是正確的。 –

相關問題