2

当我点击开始时,'秒表'开始计数..我按下暂停,只有 textview 暂停,它没有恢复,因为它仍在后台运行..我需要它在我停止的地方恢复..

这是我的点 java 代码:

package com.example.chronometer2;

import com.example.chronometer.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class ChronometerApp extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_chronometer);

       final Chronometer myChronometer = (Chronometer)findViewById(R.id.my_chronometer);
       Button buttonStart = (Button)findViewById(R.id.buttonstart);
       Button buttonStop = (Button)findViewById(R.id.buttonstop);
       Button buttonReset = (Button)findViewById(R.id.buttonreset);

       buttonStart.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.start();
   }});

       buttonStop.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.stop();

   }});

       buttonReset.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.setBase(SystemClock.elapsedRealtime());

   }});

   }
}
4

5 回答 5

4

您需要定义一个变量,其中包含计时器停止时经过的时间

timeWhenStopped = chronometer.getBase() - SystemClock.elapsedRealtime();

然后当你再次开始时设置计时器加上 timeWhenStopped

chronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);

重置计时器只需使用

chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
timeWhenStopped = 0;

也许您需要添加一个额外的变量来定义计时器是否正在运行

于 2012-10-11T08:06:31.297 回答
3

你只需要记住时间,它是可变的,例如 timeWhenStopped。然后,每次启动计时器时,您的值不是 0,而是几秒钟?您可以通过方法 setBase 设置。这里有 3 个按钮,包括开始、暂停和重新启动。祝你好运!

public class MainActivity extends Activity {
/** Called when the activity is first created. */
long timeWhenStopped = 0;

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

    final Button butStart = (Button) findViewById(R.id.button1);
    final Button butStop = (Button) findViewById(R.id.button2);
    final Button butReset = (Button) findViewById(R.id.button3);

    final Chronometer Mchronometer = (Chronometer) findViewById(R.id.chronometer1);

    butStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {                  Mchronometer.setBase(SystemClock.elapsedRealtime()
                            +timeWhenStopped);
            Mchronometer.start();
        }
    });

    butStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            timeWhenStopped = Mchronometer.getBase() - SystemClock.elapsedRealtime();
            Mchronometer.stop();
        }
    });

    butReset.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Mchronometer.setBase(SystemClock.elapsedRealtime());
            Mchronometer.stop();
            timeWhenStopped = 0;
        }
    });
    }
于 2012-12-22T13:19:44.977 回答
1

您将需要一个变量来跟踪自 Chronometer 启动以来经过的时间

查看更多:Android_Chronometer 暂停

于 2012-10-11T07:59:32.970 回答
1

@fabzy 的代码有效,但算术逻辑无效,这可能会导致问题。

mTimeWhenStopped应该是一个正数(计时器上经过的时间量),但在 fabzy 的设置中,它总是负数。如果您将mTimeWhenStopped数字用于运行计时器以外的任何事情,例如比较、显示等,这会有所不同。

以下是减法运算反转的相同代码。我还为计时器是否正在运行添加了一个可选的布尔值:

private void resumeTimer() {
    mTimer.setBase(SystemClock.elapsedRealtime() - mTimeWhenStopped);
    mTimer.start();
    mTimerIsRunning = true;

}

private void stopTimer() {
    mTimer.stop();
    mTimeWhenStopped = SystemClock.elapsedRealtime() - mTimer.getBase();
    mTimerIsRunning = false;
}

private void resetTimer() {
    mTimer.stop();
    mTimer.setBase(SystemClock.elapsedRealtime());
    mTimeWhenStopped = 0;
    mTimerIsRunning = false;
}
于 2014-04-07T00:51:08.963 回答
0

我为此做了一PauseableChronometer堂课。

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

public class PausableChronometer extends Chronometer {

    private long timeWhenStopped = 0;

    public PausableChronometer(Context context) {
        super(context);
    }

    public PausableChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PausableChronometer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void start() {
        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);
        super.start();
    }

    @Override
    public void stop() {
        super.stop();
        timeWhenStopped = getBase() - SystemClock.elapsedRealtime();
    }

    public void reset() {
        stop();
        setBase(SystemClock.elapsedRealtime());
        timeWhenStopped = 0;
    }

    public long getCurrentTime() {
        return timeWhenStopped;
    }

    public void setCurrentTime(long time) {
        timeWhenStopped = time;
        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);
    }
}
于 2014-10-20T15:22:56.557 回答