0

我正在为 Android 制作秒表应用程序。我想要单击停止时停止的时间,以及再次单击开始时要恢复的时间。我正在使用http://www.goldb.org/stopwatchjava.html类。目前,单击“开始”按钮会恢复时间,但是当我第二次单击“停止”时,它会显示错误的时间。再次在开始按钮上,时间正确恢复。这是我的代码:

Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
        case MSG_START_TIMER:
            timer.start(); //start timer
            mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
            break;

        case MSG_UPDATE_TIMER:

            time = timer.getElapsedTime() + timeStoped;

            hh = time / 3600000;
            hours.setText("" + formatter.format(hh));
            time = time - hh * 3600000;

            mm = time / 60000;
            minutes.setText("" + formatter.format(mm));
            time = time - mm * 60000;

            ss = time / 1000;
            seconds.setText("" + formatter.format(ss));
            time = time - ss * 1000;

            millis.setText("" + formatter.format(time / 10));

            mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
            break;      
                                                                             //though the timer is still running
        case MSG_STOP_TIMER:

            mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.
            timer.stop();//stop timer

            time = timer.getElapsedTime();
            timeStoped = timeStoped + time;

            hh = time / 3600000;
            hours.setText("" + formatter.format(hh));
            time = time - hh * 3600000;

            mm = time / 60000;
            minutes.setText("" + formatter.format(mm));
            time = time - mm * 60000;

            ss = time / 1000;
            seconds.setText("" + formatter.format(ss));
            time = time - ss * 1000;

            millis.setText("" + formatter.format(time / 10));

            break;

        default:
            break;
        }
    }
};

按钮 Start/Stop 的监听器:

startBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (!startIsPressed) {
                mHandler.sendEmptyMessage(MSG_START_TIMER);
                startIsPressed = true;
                startBtn.setText(R.string.stop);
            } else {
                startIsPressed = false;
                startBtn.setText(R.string.start);
                mHandler.sendEmptyMessage(MSG_STOP_TIMER);
            }
        }
    });
4

2 回答 2

2

解决方法是更换:

    time = timer.getElapsedTime();
    timeStoped = timeStoped + time;

和:

    time = timer.getElapsedTime() + timeStopped;
    timeStopped = time;
于 2012-05-13T08:13:40.550 回答
0

以小时:分钟:秒格式设置时间的更简单方法可能是:

Format formatter = new SimpleDateFormat("hh mm ss SSS");
String[] parsedTime = formatter.format(time).split(" ");
hours.setText(parsedTime[0]);
minutes.setText(parsedTime[1]);
seconds.setText(parsedTime[2]);
millis.setText(parsedTime[3]);

为了在多次停止后保持准确的时间,您需要更改此设置:

        time = timer.getElapsedTime();
        timeStoped = timeStoped + time;

对于检查以前经过的东西time,可能是这样的:

        time = timer.getElapsedTime() + time;
        timeStoped = timeStoped + time;

这些与以下内容相同:

        time += timer.getElapsedTime();
        timeStoped += time;
于 2012-05-12T16:26:08.857 回答