如何设置计时器的开始时间 [英] How can I set the start time for a chronometer

查看:226
本文介绍了如何设置计时器的开始时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有计时器的应用程序,我需要将开始时间设置为与日期之间的差值

I have an application that has a chronometer, I need to set the start time to a difference between to dates

我该怎么做?

推荐答案

一种方法是扩展Chronometer类……类似这样:

One way of doing this is to extends the Chronometer class... something like this:

public class MyChronometer extends Chronometer {

    public int msElapsed;
    public boolean isRunning = false;

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

    public int getMsElapsed() {
        return msElapsed;
    }

    public void setMsElapsed(int ms) {
        setBase(getBase() - ms);
        msElapsed  = ms;
    }

    @Override
    public void start() {
        super.start();
        setBase(SystemClock.elapsedRealtime() - msElapsed);
        isRunning = true;
    }

    @Override
    public void stop() {
        super.stop();
        if(isRunning) {
            msElapsed = (int)(SystemClock.elapsedRealtime() - this.getBase());
        }
        isRunning = false;
    }
}

您可以使用setMsElapsed(int ms)指定偏移量.您可能必须将Dates转换为Long,然后从那里进行数学运算.如果您指定类所在的整个包,则可以在XML布局中使用该类.

You can use setMsElapsed(int ms) to specify your offset. You will probably have to convert your Dates to Long and do the math from there. The class can be used in XML layout if you specify the whole package where the class resides.

这篇关于如何设置计时器的开始时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆