Java 以 00:00 格式创建以秒为单位的递增计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2536882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an incrementing timer in seconds in 00:00 format?
提问by Donal Rafferty
I want to create an incrementing second timer like a stopwatch.
我想创建一个像秒表一样递增的第二个计时器。
So I want to be able to display the seconds and minutes incrementing in the format 00:01...
所以我希望能够显示以 00:01 格式递增的秒和分钟...
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?
Google 只提供 24 小时制示例,我想知道有人可以让我开始使用我想要做什么的示例或教程吗?
Edit:
编辑:
Here is what I have using the Chronometer in Android so far
到目前为止,这是我在 Android 中使用 Chronometer 的内容
In onCreate()
在 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);
}
});
In onDestroy()
在 onDestroy()
@Override
public void onDestroy() {
inCall = false;
elapsedTimeBeforePause = SystemClock.elapsedRealtime() - stopWatch.getBase();
super.onDestroy();
}
The above compiles and runs but the TextView never increments, it always stays at 0, can anyone see why?
上面编译并运行,但 TextView 永远不会增加,它总是保持在 0,有人能明白为什么吗?
采纳答案by Steve Haley
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.
我假设您不知道 Android Chronometer
- 它已经具有基本的秒表功能。您需要稍微处理一下它的特性,但是一旦您了解它的工作原理,就不难让它做您想做的事情。
There are a few ways that time is calculated on the phone, but the two main ones are:
有几种方法可以在手机上计算时间,但主要有两种:
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.
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.
“实时”,比如现在根据我的电脑时钟,是英国的上午 11 点 23 分。但是,如果我的计算机联系时间服务器并被告知时间错误,或者如果我带着笔记本电脑旅行并跨越了时区边界,这可能会改变。使用它会对您的秒表造成严重破坏,因为测量的时间可能随时发生变化。
“开机后经过的时间”,即手机开机后经过的毫秒数。这个数字与实际时间没有任何关系,但它会以完全可预测的方式运行。这就是 Android Chronometer 所使用的。
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:
在Chronometer
本质上是一种“向上计数”定时器,比较当前SystemClock.elapsedRealtime()
靠在elapsedRealtime(),将其设置FOT其基准时间。两者之间的差值除以 1000,就是自计时器启动以来的秒数。但是,如果您停止计时器然后再次启动它,您将得到一个违反直觉的结果 - 计时器将显示经过的时间,就好像它从未停止过一样。这是因为您需要调整它的基准时间以考虑它停止的时间。这很简单:
// 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);
Edit: 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.
编辑:这是基本秒表的完整代码,它在 TextView 中显示您的时间,而不是在您的 XML 文件中声明的 Chronometer 小部件。
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();
}
}
In your main.xml you need to have this
在你的 main.xml 你需要有这个
<Chronometer android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chrono"
android:visibility="gone"/>
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.
毫无疑问,有一种方法可以让 Chronometer 工作而无需在 XML 文件中声明它,但是构造函数Chronometer stopwatch = new Chronometer(this);
没有正常工作。
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! :)
上面的代码以一种非常基本的方式显示了经过的时间。例如,如果只过去了 5 秒,它将显示0:5
而不是0:05
您可能想要的。解决这个问题并不难,但我会把它留给你解决!:)
回答by jitter
How about this one Digital Clock.
这个数字时钟怎么样。
But basically you should be able to find many java implementations by googling for keywords like java, stopwatch, digital clock, ...
但基本上你应该能够通过谷歌搜索关键字来找到许多 java 实现,比如 java、秒表、数字时钟……
You could take a look at Stopwatch Appletand modify as needed(Oops no source with this one)
你可以看看秒表小程序并根据需要进行修改(哎呀没有这个来源)