在android中创建一个简单的数字计时器
我基本上只是想跟踪从应用程序启动时的 0:00 开始的秒数和分钟数,并将其显示在 TextView 中,最好的方法是什么?
I'm basically looking to just keep track of seconds and minutes starting at 0:00 from when the app starts, and display it in a TextView what would be the best method of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当计时器启动一个非 UI 线程时,我宁愿不使用它来更新 TextView。在您的情况下,Timer 可以替换为 Handler 类。而且您还可以避免这个新的非 UI 线程的开销。 这里是一个示例:
As timer starts a non-UI Thread, i'll prefer not to use it for updating TextView. In your case Timer can be replaced by the Handler class. And you can also avoid the overhead of this new non-UI thread. Here is an example:
您可以使用 Timer 来安排 TimerTask 每秒。
开始时间 - 当前时间
),我不确定您在更新时是否会遇到问题TimerTask 作业中的 TextView,因为它不在 UI 线程中运行,在这种情况下,您可以使用 Activity.runOnUiThread 来执行此操作
You could use a Timer to schedule a TimerTask each second.
beginning time - current time
)I'm not sure if you could have problems when updating the TextView from the TimerTask job as it doesn't run in UI thread, in that case you can use Activity.runOnUiThread to do this
在你的程序中尝试一下...!
try it in ur program...!