android - 如何显示实时运行的时间?
问题描述
我想显示持续运行的时间,但这段代码只显示应用程序启动的时间。
TextView textView = (TextView) findViewById(R.id.textView);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
textView.setText(currentDateTimeString);
解决方案
您可以使用以下处理程序来执行此操作:
new Handler().postDelayed(new Runnable() {
public void run() {
// display your time here...
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
textView.setText(currentDateTimeString);
}
}, 1000); // here 1 seconds to refresh time after 1 seconds
推荐阅读
- java - 由于目录连接,Android Studio 不正确的重复类错误?
- microservices - 上传大文件时断路器打开
- android - 为什么 Fragmenttransaction replace() 的行为类似于 add()?
- javascript - 如何为 Google 地图创建“大小圆圈”图例
- mysql - 连接返回空
- flutter - InheritedWidget 无法从新路由访问
- opencl - 在 OpenCL 中,为什么我要为每台英特尔设备提供一个平台?
- c# - 如何知道 CalendarViewDayItem 超出 CalendarView 月份范围?
- python-3.x - 我如何遍历列表并使用 simplekml 创建/添加到 kml?
- excel - 如何在 VBA 上按标题名称选择特定列,并在其他操作(删除重复项、自动过滤器)中使用所选列