【问题标题】:Dateformat not updated when the time is changed更改时间时未更新日期格式
【发布时间】:2013-10-26 05:31:04
【问题描述】:

我正在开发一个 android 应用程序,其中我在我的应用程序的主屏幕上为用户显示日期和时间。我正在使用下面的代码。

        SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy");
        String strtDates = sdf.format(cal.getTime());
        textviewfordate.setText(strtDates);

        clk = (DigitalClock)findViewById(R.id.digitalClock);

但是,当时间更改时,如果不刷新屏幕,日期不会更新。例如,当时间为晚上 11:59:00 并且时间更改为上午 12:00:00 时,将不会更新日期。只有在刷新屏幕时才会更新。我希望在不刷新屏幕的情况下自动更改日期。

不知道怎么做!请帮忙!谢谢!

【问题讨论】:

    标签: android date android-activity clock simpledateformat


    【解决方案1】:

    如果您在onCreate() 中将文本设置为 textview,则它将在调用该方法时仅运行一次,并且不会在每次时间更改时更新。

    根据您的需要,您必须使用handlerexecutor 或任何其他thread,它会在每个用户定义的时间运行,并且您必须将当前时间文本设置为里面的textview他们。因此,文本将被更新。

    也可以参考以下链接,它们会帮助你:

    Android run thread in service every X seconds

    How to execute a function every two seconds in Java on Android?

    display data after every 10 seconds in Android

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 2020-08-23
      • 2013-06-29
      • 2017-05-13
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多