2014-02-21 98 views
-2

因此,當我的秒錶停止時,它顯示停止的時間,但當再次按下啓動按鈕時,它將重新啓動時間返回到0:00:00我怎麼能從它恢復時間停在?這裏是如果需要的話,整個代碼停止後恢復停止監視時間

package com.webdeveloper93.stopwatch; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import java.util.Timer; 

import android.os.Bundle; 
import android.os.Handler; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class StopWatch extends Activity implements OnClickListener { 

    private long startTime; 
    private long endTime; 
    private Random rand = new Random(); 
    private boolean timerIsRunning; 
    private String savedTime; 
    private TextView stopWatchC; 
    private Button startButton,stopButton,pauseButton; 
    private RelativeLayout mainLayout; 
    private Handler handle; 
    private Handler backHand = new Handler(); 
    private boolean paused; 
    private long UPDATE_EVERY = 200; 
    private int backgrounds[] = {R.drawable.woman_1,R.drawable.woman_2,R.drawable.woman_3,R.drawable.woman_4}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.stopwatch); 
     stopWatchC = (TextView) findViewById(R.id.counter); 
     startButton = (Button) findViewById(R.id.start_button); 
     stopButton = (Button) findViewById(R.id.stop_button); 
     pauseButton = (Button) findViewById(R.id.pause); 
     mainLayout = (RelativeLayout) findViewById(R.id.main_layout); 
     //ONCLICK HANDLERS 
     startButton.setOnClickListener(this); 
     stopButton.setOnClickListener(this); 
     pauseButton.setOnClickListener(this); 
     backHand.postDelayed(backgroundUpdate, 300); 

    } 
    /** 
    * Handles displaying the counter 
    */ 
    public void SWCounterDisplay() 
    { 
    String display; 
    long now; 
    long difference; 
    long secs; 
    long mins; 
    long hours; 

    if(timerIsRunning == true) 
    { 
     now = System.currentTimeMillis(); 
    }else{ 
     now = endTime; 
    } 

    difference = now-startTime; 
    //No negative numbers 
    if(difference < 0) 
    { 
     difference = 0; 
    } 

    secs = difference/1000; 
    mins = secs/60; 
    hours = mins/60; 
    secs = secs%60; 
    mins = mins%60; 

    display = String.format("%d", hours) + ":" + 
       String.format("%02d",mins) + ":" + 
       String.format("%02d", secs); 


    stopWatchC.setText(display); 

    } 
    public void pauseTimer() 
    { 
     timerIsRunning = false; 
     endTime = System.currentTimeMillis(); 
     startButton.setEnabled(true); 
    } 
    /** 
    * Starts the stop watch 
    */ 
    public void startTimer() 
    { 
     timerIsRunning = true; 
     stopButton.setEnabled(timerIsRunning); 
     startButton.setEnabled(false); 
     startTime = System.currentTimeMillis(); 
     //Create new handler 
     handle = new Handler(); 
     handle.postDelayed(timerUpdate, UPDATE_EVERY); 

    } 
    /** 
    * Stops the timer 
    */ 
    public void stopTimer() 
    { 
     timerIsRunning = false; 
     stopButton.setEnabled(timerIsRunning); 
     startButton.setEnabled(true); 
     endTime = System.currentTimeMillis(); 
     handle.removeCallbacks(timerUpdate); 
     handle = null; 
    } 
    /** 
    * Handles any onclick events 
    */ 
    @Override 
    public void onClick(View v) { 

     if(v == startButton) 
     { 
      startTimer(); 
     }else if(v == stopButton) 
     { 
      stopTimer(); 
     }else if(v == pauseButton) 
     { 
      pauseTimer(); 
     } 
    } 


    private Runnable backgroundUpdate = new Runnable(){ 

     @Override 
     public void run() { 
      mainLayout.setBackgroundResource(backgrounds[rand.nextInt(backgrounds.length)]); 
      backHand.postDelayed(this, 20000); 
     } 

    }; 
    /** 
    * Handles updating the timer 
    */ 
    private Runnable timerUpdate = new Runnable(){ 

     @Override 
     public void run() { 
      SWCounterDisplay(); 
      if(handle != null){ 
       //Log.d("Run Method","RUN METHOD IS RUNNING"); 
      handle.postDelayed(this, UPDATE_EVERY); 
      } 

     } 

    }; 
    /** 
    * Call run method if timer is still running 
    */ 
    public void onStart() 
    { 
     super.onStart(); 
     if(timerIsRunning == true) 
     { 
      handle = new Handler(); 
      handle.postDelayed(timerUpdate, UPDATE_EVERY); 
     } 
    } 
    /** 
    * Stop the timer if timer is still running 
    */ 
    public void onStop() 
    { 
     super.onStop(); 
     if(timerIsRunning == true) 
     { 
      handle.removeCallbacks(timerUpdate); 
      handle = null; 
     } 
    } 
    /** 
    * Resume when the onResume method is called 
    */ 
    public void onResume() 
    { 
     super.onResume(); 
     stopButton.setEnabled(false); 
     startButton.setEnabled(true); 
     SWCounterDisplay(); 
    } 

} 

回答

0

的問題是你在startTimer方法重置startTime

您可能需要某種狀態標誌的,可確定秒錶之前已經開始,但不休息。

這將需要你增添重置選項休息的標誌

例如「被前開始」 ...

public void startTimer() 
{ 
    timerIsRunning = true; 
    stopButton.setEnabled(timerIsRunning); 
    startButton.setEnabled(false); 
    if (!previouslyStarted) { 
     previouslyStarted = true; 
     startTime = System.currentTimeMillis(); 
    } 
    //Create new handler 
    handle = new Handler(); 
    handle.postDelayed(timerUpdate, UPDATE_EVERY); 

} 

public void resetTimer() { 
    previouslyStarted = false; 
    // reset the time values and output as you want... 
} 

更新

我已經沒有測試過,所以你可能需要調整它,但基本上,你需要保持一種「運行時間」的價值,這可以包括在你的計算之間的開始,停止和重置事件...

public void startTimer() { 
    timerIsRunning = true; 
    stopButton.setEnabled(timerIsRunning); 
    startButton.setEnabled(false); 

    if (!previouslyStarted) { 
     runTime = 0; 
     previouslyStarted = true; 
    } 

    startTime = System.currentTimeMillis(); 
    //Create new handler 
    handle = new Handler(); 
    handle.postDelayed(timerUpdate, UPDATE_EVERY); 

} 

public void stopTimer() { 
    timerIsRunning = false; 
    stopButton.setEnabled(timerIsRunning); 
    startButton.setEnabled(true); 
    endTime = System.currentTimeMillis(); 

    runTime += endTime - startTime; 

    handle.removeCallbacks(timerUpdate); 
    handle = null; 
} 

public void resetTimer() { 
    previouslyStarted = false; 
    // reset the time values and output as you want... 
} 

而在你SWCounterDisplay方法,你需要你的calcualtions中包括這一點,例如...

difference = runTime + (now - startTime); 
+0

確定此解決方案有效中途然而,當我做恢復它像它仍處於計數後臺暫停。根據停頓的時間長短,最後會增加幾秒鐘,分鐘等等。當我重新啓動 – user3325917

+0

時,計時器不會依賴開始/結束時間,而是需要保持一個額外的值,這是「運行時間」。因此,每次您暫停/恢復秒錶時,您都需要重置'startTime'時間並將結果值添加到「運行時間」中,因此「運行時間」將成爲您計算的基準... – MadProgrammer

+0

Can你請舉個例子嗎?我是一個視覺化的人,所以我更容易用一個例子來理解。再次感謝您的幫助 – user3325917