开发者

Check if Chronometer is running

chronometer in android how to check whether chronometer is running or stop? if start then i want to stop it and if not running then start chrono开发者_如何转开发meter.


You can check this using boolean variable.when you start chronometer you set boolean variable true and when it stop you set boolean variable false.

boolean isChronometerRunning = false;
if (true)  // condition on which you check whether it's start or stop
{
    chronometer.start();
    isChronometerRunning  = true;
}
else
{
  chronometer.stop();
  isChronometerRunning  = false;
}


You can extend Chronomter, like this:

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

public class MyChronometer extends Chronometer {

    private boolean isRunning = false;

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

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

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

    @Override
    public void start() {
        super.start();
        isRunning = true;
    }

    @Override
    public void stop() {
        super.stop();
        isRunning = false;
    }

    public boolean isRunning() {
        return isRunning;
    }

}

And then just call isRunning().


It is strange that it doesn't expose that property. I don't really see an ideal way to check for it beyond keeping track on your own.

You could just take the source code for that class, implement it in your project yourself, and add a method like this:

public boolean getStarted() {
    return mStarted;
}


private boolean isChronometerRunning = false;

private Chronometer chronometer;

chronometer = (Chronometer) findViewById(R.id.chronometer);

chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();

isChronometerRunning = true;

Now whenever want to stop chronometer use the below code for checking chronometer is running or not.

    if (isChronometerRunning){
        chronometer_call.stop();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜