00:00格式创建以秒为单位递增定时器?定时器、单位、格式

2023-09-03 22:40:32 作者:范er的很文艺

我想创建一个递增秒计时器类似于秒表。

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...

谷歌不仅带来上涨了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中至今

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 天文钟 - 它已经具备了基本的秒表功能。你需要有其特殊性一点的工作,但它不是很难得到它做你想要什么,一旦你了解它是如何工作的。

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:

在实时,如现在根据我的计算机时钟,这是日上午11:23在英国。然而,这可能如果我的计算机联系更改时间服务器,并告诉它有错误的时间,或者如果我在旅​​行用笔记本电脑和跨越时区边界。使用这将严重破坏你的秒表作为测量的时间可能会随时发生变化。

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.

,这是毫秒数,因为电话被接通的启动以来经过的时间。这个数字不承担任何关系的实际时间,它是,但它会表现在完全predictable方式。这是Android天文台使用什么样的。

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.

天文台表本质上是一种计数定时器,比较当前 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);

编辑:这里是全code的基本秒表,显示你的时间在一个TextView在你的XML文件,而不是记时计小部件声明

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"/>

还有无疑是一种方式来获得的天文台给未声明的XML文件中工作,但构造精密计时秒表=新的天文台表(本); 没有工作正常。

以上code显示经过的时间在一个非常基本的方法。例如,如果只有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! :)

 
精彩推荐
图片推荐