Android:在文本视图中显示/更新时钟
我想在我的应用程序中显示和更新时钟。显示的时间是通过网络服务获取的。
现在我如何每秒/分钟更新文本视图?还有如何保持时间同步?
I want to display and update a clock in my app. The time to be displayed is taken via a webservice.
Now how do I update the textview each second/minute? And also how do I keep the time synchronized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的时间已经从网络服务中获取,因此您应该使用带有
Service
的Timer
以固定速率(例如每秒、每分钟等)继续获取它,或者AsyncTask
实现。来更新
TextView
的内容您可以通过
R.id.myTextView
引用的您在中定义的
TextView
布局 xml,
formattedTime
是时间你已经从网络服务中获得了,并且
将其格式化为可读。
Since your time is already taken from a webservice, you should keep taking it at a fixed rate (like every second, minute, etc.) using a
Timer
with aService
or anAsyncTask
implementation.You can update your
TextView
's content byWhere
R.id.myTextView
refers to the id ofyour
TextView
defined in yourlayout xml, and
formattedTime
is the time thatyou've got from the web service, and
formatted it to be readable.
您可以运行一个线程并让它休眠所需的分钟/秒数(从毫秒转换后)。然后通过线程通过处理程序更新此文本,该处理程序会为您更新它。然而;您可以使用的一件事是系统时钟来为您执行此操作(如果它们相同)。
You could have a thread running and have it sleep the required number of minutes/seconds (after being converted from ms). And then update this text through the thread via a handler that updates it for you. However; one thing you can potentially use is the system clock to do this for you (If they are the same).