1

我有两个按钮“开始”和“暂停”和一个 Textview。

我遇到的问题是当我按下暂停按钮然后按下开始按钮时,秒表不会从停止的地方开始。

谢谢您的帮助

public class StopWatch extends Activity {
    /** Called when the activity is first created. */

    private TextView timeflg;
    private Button startButton;
    private Button pauseButton;
    private long mStartTime = 0L;
    private Handler mHandler = new Handler();
    long millis = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (mStartTime == 0L) {
            mStartTime = SystemClock.uptimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 0);


            }

            timeflg = (TextView) findViewById(R.id.timeflag);

            startButton = (Button) findViewById(R.id.start);
            startButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){

                    mHandler.postDelayed(mUpdateTimeTask, 0);

                }
            });

            pauseButton = (Button) findViewById(R.id.pause);
            pauseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){

                    mHandler.removeCallbacks(mUpdateTimeTask);

                }
            });

    }

    private Runnable mUpdateTimeTask = new Runnable() {
            public void run() {

                    millis = SystemClock.uptimeMillis() - mStartTime;

                    int seconds = (int) (millis / 1000);
                    int minutes = seconds / 60;
                    seconds = seconds % 60;
                    int milliseconds = (int) (millis%1000);
                    timeflg.setText("" + minutes + ":" + String.format("%02d",seconds)+ ":" 
                    + String.format("%03d",milliseconds));
                    mHandler.postDelayed(this,0);
            }

    };



}
4

1 回答 1

6

有问题:

millis = SystemClock.uptimeMillis() - mStartTime;

mStartTime在创建活动时初始化。然后从 mStartTime 开始计算呈现时间。

编辑:
我看你还是不明白。

按下开始按钮时初始化 mStartTime,它是当前经过的开始时间:

        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                mStartTime = SystemClock.uptimeMillis();
                mHandler.postDelayed(mUpdateTimeTask, 0);
            }
        });

计算 mCurrenLapse 时间。显示时间是过去经过的时间和当前经过的时间:

    private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
                mCurrentLapse = SystemClock.uptimeMillis() - mStartTime;
                //
                long displayTime = mTime + mCurrentLapse;
                .
                .
                .

总时间是所有流逝的时间,当单击暂停时,当前时间流逝将添加到总时间中:

        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                mTime += mCurrentLapse;
                mHandler.removeCallbacks(mUpdateTimeTask);
            }
        });
于 2012-06-10T19:02:23.300 回答