本文介绍了以 00:00 格式创建以秒为单位的递增计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个像秒表一样递增的第二个计时器.

I want to create an incrementing second timer like a stopwatch.

所以我希望能够显示以 00:01 格式递增的秒和分...

So I want to be able to display the seconds and minutes incrementing in the format 00:01...

Google 只提供 24 小时制示例,我想知道有人可以让我开始学习我想做的事情的示例或教程吗?

Google only brings up 24 hour clock examples, I was wondering could anyone get me started with an example or tutorial of what I want to do?

这是我目前在 Android 中使用 Chronometer 的情况

Here is what I have using the Chronometer in Android so far

在 onCreate()

    secondsT = 0;
    elapsedTimeBeforePause = 0;

    stopWatch.start();
    startTime = SystemClock.elapsedRealtime();
    stopWatch.setBase(elapsedTimeBeforePause);

     stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                //countUp is a long declared earlier
                secondsT = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (secondsT / 60) + ":" + (secondsT % 60);
                //textGoesHere is a TextView
                ((TextView)findViewById(R.id.time)).setText(asText);
            }
     });

在 onDestroy()

@Override
public void onDestroy() {

    inCall = false;
    elapsedTimeBeforePause = SystemClock.elapsedRealtime() - stopWatch.getBase();

    super.onDestroy();
}

上面编译并运行但TextView从不增加,它总是保持在0,有人知道为什么吗?

The above compiles and runs but the TextView never increments, it always stays at 0, can anyone see why?

推荐答案

我假设您不知道 Android Chronometer - 它已经具有基本的秒表功能.您需要稍微处理一下它的特性,但是一旦您了解它的工作原理,就不难让它做您想做的事情.

I'm assuming you aren't aware of the Android Chronometer - it already has a basic stopwatch function. You need to work with its peculiarities a bit, but it's not hard to get it to do what you want once you understand how it works.

在手机上计算时间的方法有几种,但主要有两种:

There are a few ways that time is calculated on the phone, but the two main ones are:

  1. 实时",比如现在根据我的电脑时钟,是英国的上午 11 点 23 分.但是,如果我的计算机联系时间服务器并被告知时间错误,或者如果我带着笔记本电脑旅行并跨越了时区边界,这可能会改变.使用此功能会对您的秒表造成严重破坏,因为测量的时间可能随时更改.

  1. The "real time", such as right now according to my computer clock, it is 11:23am in England. However, this can change if my computer contacts a time server and is told it has the wrong time, or if I were travelling with a laptop and crossed a timezone boundary. Using this would wreak havoc with your stopwatch as the measured time could change at any time.

开机后经过的时间",即手机开机后经过的毫秒数.这个数字与它的实际时间没有任何关系,但它会以完全可预测的方式运行.这就是 Android Chronometer 所使用的.

The "elapsed time since boot", which is the number of milliseconds since the phone was switched on. This number doesn't bear any relation to the real time it is, but it will behave in a perfectly predictable manner. This is what the Android Chronometer uses.

Chronometer 本质上是一个计数"计时器,将当前的 SystemClock.elapsedRealtime() 与为其基础时间设置的 elapsedRealtime() 进行比较.两者之间的差值除以 1000,就是自计时器启动以来的秒数.但是,如果您停止计时器然后再次启动它,您将得到一个违反直觉的结果 - 计时器将显示经过的时间,就好像它从未停止过一样.这是因为您需要调整它的基本时间以考虑它停止的时间.这很简单:

The Chronometer is essentially a 'count up' timer, comparing the current SystemClock.elapsedRealtime() against the elapsedRealtime() that was set fot its base time. The difference between the two, divided by 1000, is the number of seconds since the timer was started. However, if you stop the timer and then start it again, you will get a counter-intuitive result - the timer will show the elapsed time as if it had never stopped. This is because you need to adjust its base time to take into consideration the time it was stopped. This is simple to do:

// When you're stopping the stopwatch, use this
// This is the number of milliseconds the timer was running for
elapsedTimeBeforePause = SystemClock.elapsedRealtime() - timer.getBase();

// When you're starting it again:
timer.setBase(SystemClock.elapsedRealtime() - elapsedTimeBeforePause);

这是基本秒表的完整代码,它在 TextView 中显示您的时间,而不是在您的 XML 文件中声明的 Chronometer 小部件.

Here is the full code for a basic stopwatch, which displays your time in a TextView rather than the Chronometer widget declared in your XML file.

public class TestProject extends Activity {
    TextView textGoesHere;
    long startTime;
    long countUp;

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

        Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);
        startTime = SystemClock.elapsedRealtime();

        textGoesHere = (TextView) findViewById(R.id.textGoesHere);
        stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (countUp / 60) + ":" + (countUp % 60);
                textGoesHere.setText(asText);
            }
        });
        stopWatch.start();
    }
}

在你的 main.xml 你需要有这个

In your main.xml you need to have this

<Chronometer android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chrono"
    android:visibility="gone"/>

毫无疑问,有一种方法可以让 Chronometer 工作而无需在 XML 文件中声明它,但是构造函数 Chronometer stopwatch = new Chronometer(this); 无法正常工作.

There's undoubtedly a way to get the Chronometer to work without declaring it in the XML file, but the constructor Chronometer stopwatch = new Chronometer(this); didn't work properly.

上面的代码以一种非常基本的方式显示了经过的时间.例如,如果只过去了 5 秒,它将显示 0:5 而不是您可能想要的 0:05.解决这个问题并不难,但我会把它留给你解决!:)

The above code displays the elapsed time in a very basic way. For example, if only 5 seconds have gone by, it will show 0:5 rather than the 0:05 you probably want. Fixing that is not hard to do, but I'll leave that for you to work out! :)

这篇关于以 00:00 格式创建以秒为单位的递增计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 23:43