使用 Android Chronometer 显示毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2979835/
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
Show milliseconds with Android Chronometer
提问by Matthew Steeples
I'm looking for a way to make the Chronometer in Android (preferably 1.6 and upwards) show 10ths of a second while counting up.
我正在寻找一种方法让 Android 中的 Chronometer(最好是 1.6 及更高版本)在计数时显示十分之一秒。
Is it possible to do this? If not, is there a free (and preferably open source) library that does the same? Failing that I'll write my own, but I'd rather use someone else's!
是否有可能做到这一点?如果没有,是否有一个免费的(最好是开源的)库可以做同样的事情?失败了我会写我自己的,但我宁愿使用别人的!
采纳答案by CommonsWare
Is it possible to do this?
是否有可能做到这一点?
Not really. You could pass a format string that shows tenths of a second, but the Chronometer
itself only updates every second. The update frequency is baked into the code.
并不真地。您可以传递一个显示十分之一秒的格式字符串,但它Chronometer
本身仅每秒更新一次。更新频率已写入代码。
If not, is there a free (and preferably open source) library that does the same?
如果没有,是否有一个免费的(最好是开源的)库可以做同样的事情?
The Chronometer
sourceis available under the Apache License 2.0, so you can modify it to suit. Find all occurrences of 1000
and change them to 100
, and you're probably most of the way there.
该Chronometer
源代码在 Apache 许可证 2.0 下可用,因此您可以对其进行修改以适应。找到所有出现的1000
并将它们更改为100
,您可能已经完成了大部分工作。
Bear in mind that this Chronometer
can be used in an activity but not an app widget, since customized View
classes are not supported by the app widget framework.
请记住,这Chronometer
可以在活动中使用,但不能在应用小部件中使用,因为View
应用小部件框架不支持自定义类。
回答by antoniom
回答by Bharat Ahuja
I created an instance of a class that I built inside the Activity
. This new class was an extension of the Android CountDownTimer
class(add the implemented methods)
我创建了一个在Activity
. 这个新类是AndroidCountDownTimer
类的扩展(添加实现的方法)
Here in the constructor you can add the duration in milliseconds and the interval to be 1/10 of a second, that is 100 milliseconds. In the onTick
method, the commands are executed every 1/100th of a second for the given duration. That should work.
在构造函数中,您可以添加以毫秒为单位的持续时间和间隔为 1/10 秒,即 100 毫秒。在该onTick
方法中,命令在给定的持续时间内每 1/100 秒执行一次。那应该工作。
回答by Syed Hissaan
TimeCounteris a library that does exactly the same. You won't have to worry about adding and subtracting time when you pause/resume your stop-watch. I am sure this will make your day.
TimeCounter是一个功能完全相同的库。当您暂停/恢复秒表时,您不必担心增加和减少时间。我相信这会让你开心。
回答by bboylalu
I have found the way after lots and lots of research. I don't know if it's effective or not, because the truth is that the Emulator crashes after about 10 seconds, but on the phone it runs just fine, and that does it for me. It looks something like this:
经过大量的研究,我找到了方法。我不知道它是否有效,因为事实是模拟器在大约 10 秒后崩溃,但在电话上它运行得很好,这对我来说是有效的。它看起来像这样:
import android.os.Handler;
public class Chronometer extends Activity {
private Handler mHandler = new Handler();
public void actions() {
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 1); //1 is the number of milliseconds you want the text to be updated
}
Runnable mUpdateTimeTask = new Runnable() {
public void run() {
i++;
txt.setText(formatTime(i)); // formatTime is just for make it readable in HH:MM:SS:MS, but I assume you already have this
mHandler.postDelayed(this, 1);
}
};
}