停止后恢复停止观看时间

时间:2014-02-21 00:06:04

标签: java android

所以当我的秒表停止时,它显示停止的时间,但是当再次按下开始按钮时,它会重新启动时间回到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();
    }

}

1 个答案:

答案 0 :(得分:0)

问题是您要重置startTime方法

中的startTimer

您可能需要某种状态标志,以确定秒表是否先前已经开始但未休息。

这将要求你添加一个重置选项来休息“之前已启动”标志

例如......

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方法中,您需要将其包含在计算中,例如......

difference = runTime + (now - startTime);