计时表简历?

时间:2012-10-11 07:52:05

标签: java android chronometer

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

这是我的dot 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());

   }});

   }
}

5 个答案:

答案 0 :(得分:4)

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

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

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

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

重置计时器只需使用

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

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

答案 1 :(得分: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;
        }
    });
    }

答案 2 :(得分:1)

  

你需要一个跟踪时间的变量   自天文台开始以来已经过去了

查看更多:Android_Chronometer pause

答案 3 :(得分: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;
}

答案 4 :(得分: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);
    }
}