首页 > 解决方案 > 如何显示实时运行的时间?

问题描述

我想显示持续运行的时间,但这段代码只显示应用程序启动的时间。

TextView textView = (TextView) findViewById(R.id.textView);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
textView.setText(currentDateTimeString);

标签: android

解决方案


您可以使用以下处理程序来执行此操作:

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

推荐阅读