不知道如何在我的Android秒表上说出我的重置动作

时间:2015-03-09 14:52:22

标签: java android reset stopwatch

我已经尝试了很多方法来解决我的问题,但我对编码很新,所以我需要有关如何正确理解Word'我的秒表中重置秒表的动作。所有它需要做的是以某种方式将当前值重置回00:00:00,就像计时器启动之前一样,但我不确定如何去做...这是部分我挣扎的XML:



resetButton = (Button) findViewById(R.id.reset);
        return timeInMillies = startTime;
        resetButton.setOnClickListener(SystemClock.setCurrentTimeMillis(timeInMillies = 00:00:00))
        long timeInMillies = 0L;
        long timeSwap = 0L;
        long finalTime = 0L;



 然而,其他一切都很好,就像一个魅力。如果它可以帮助你,那么它就是整个XML:



package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;

import android.widget.TextView;


public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    private Button startButton;
    private Button pauseButton;
    private Button resetButton;
    private long startTime = 0L;
    private Handler myHandler = new Handler();
    long timeInMillies = 0L;
    long timeSwap = 0L;
    long finalTime = 0L;

    private TextView textTimer;


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

        textTimer = (TextView) findViewById(R.id.textTimer);

        startButton = (Button) findViewById(R.id.start);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                myHandler.postDelayed(updateTimerMethod, 0);
            }
        });

        pauseButton = (Button) findViewById(R.id.stop);
        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                timeSwap += timeInMillies;
                myHandler.removeCallbacks(updateTimerMethod);

            }
        });

        resetButton = (Button) findViewById(R.id.reset);
        return timeInMillies = startTime;
        resetButton.setOnClickListener(SystemClock.setCurrentTimeMillis(timeInMillies = 00:00:00))
        long timeInMillies = 0L;
        long timeSwap = 0L;
        long finalTime = 0L;



    }
    private Runnable updateTimerMethod = new Runnable() {

        public void run() {
            timeInMillies = SystemClock.uptimeMillis() - startTime;
            finalTime = timeSwap + timeInMillies;

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

    };

}




非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

对于重置按钮,您需要更改两件事:

1)文本域。

2)保持startTime和timeSwap的变量。

但是你不想改变应用程序的阶段,我会用以下方式来做:

   resetButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            textTimer.setText("" + 00 + ":"
                    + String.format("%02d", 00) + ":"
                    + String.format("%03d", 00));
            startTime = SystemClock.uptimeMillis();
            timeSwap=0;
        }
    });