简体   繁体   English

如何在 Android Studio 中将实时 DateTime 显示为 textView(如数字手表)?

[英]How to show the live DateTime as a textView (Like a digital watch) in Android Studio?

I have made two textView in an xml file where one textView shows current date and another shows current time as text .我在xml文件中制作了两个textView文件,其中一个textView显示当前日期,另一个显示当前时间为text

Now, I have used the java Calendar.getInstance().getTime() method to get the date & time information.现在,我使用了 java Calendar.getInstance().getTime()方法来获取日期和时间信息。 But it is acting as a static view ie it is not changing the date & time like a digital clock.但它充当 static 视图,即它不会像数字时钟那样更改日期和时间。

Now I am trying to show the textViews to show the current date & time in synchronization with device-system's date & time.现在我正在尝试显示textViews以显示与设备系统的日期和时间同步的当前日期和时间。 That means, suppose now it is 11:59:59 PM in the night and the date is 7th Sep, 2022 .这意味着,假设现在是晚上11:59:59 PM ,日期是7th Sep, 2022 Just after 1s , my time textView should show the time as 12:00:00 AM and date should show as 8th Sep, 2022 .就在1s之后,我的时间textView应该显示时间为12:00:00 AM ,日期应该显示为8th Sep, 2022 And it should continue to change the time after every 1s like a digital clock.并且它应该像数字时钟一样在每1s秒后继续更改时间。 Last of all, there should not be any delay in between system dateTime & app dateTime ie perfectly synchronised.最后,系统日期时间和应用程序日期时间之间不应有任何延迟,即完美同步。

How to do that??怎么做??

public class MainActivity extends AppCompatActivity {

    TextView textDate, textClock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textDate = findViewById(R.id.textDate);
        textClock = findViewById(R.id.textClock);
        setDateTime();
    }

    private void setDateTime() {
        Date c = Calendar.getInstance().getTime();
        SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy", Locale.getDefault());
        SimpleDateFormat df_clock = new SimpleDateFormat("hh:mm:ss a", Locale.getDefault());
        String formattedDate = df.format(c);
        String formattedClock = df_clock.format(c);

        textDate.setText(formattedDate);
        textClock.setText(formattedClock);
    }
}

There's already a TextClock class in Android which you should be able to use. Android 中已经有一个TextClock class ,您应该可以使用它。 Try replacing your two TextView s with these and set the formats to your patterns.尝试用这些替换您的两个TextView 并将格式设置为您的模式。

You need to set up a background thread that will ask the UI to update itself every second.您需要设置一个后台线程,该线程将要求 UI 每秒更新一次。

I think something like this (but I don't know how accurate this will be)我想是这样的(但我不知道这有多准确)

private void blink() {
      final Handler hander = new Handler();
      new Thread(new Runnable() {
         @Override
         public void run() {
            // off the UI
            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            // back ot the UI
            hander.post(new Runnable() {
               @Override
               public void run() {
                  // notify the UI here
                  blink();
               }
            });
         }
      }).start();
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android studio - Textview 显示日期 - Android studio - Textview show date 如何在 Textview 中显示数据 2 秒(android Studio)? - How do I show data in Textview for a 2 second (android Studio)? 我想在textView中显示线程的计时器(实时) - I would like to show the timer of a thread in textView (Live) 使用 Android Studio 编写应用程序以观看由相机提供的实时视频 - Writing an App with Android Studio to watch a Live Video which is provided by a Camera java代码:如何在android中流式传输直播频道以观看直播 - java Code : how to stream live channels in android to watch live Android Studio,点击时观看面孔-显示画布2,隐藏canvas1 - Android Studio, Watch Face On tap - show Canvas 2, hide canvas1 如何在Android studio java的textview中显示另一个类的变量? - How can I show a variable from another class in a textview in Android studio java? Android Studio:如何使用 Textview 始终显示更新变量? - Android studio: How do I show an updating variable at all times using a Textview? 如果在 Android Studio 中有一个或多个为空,如何从多个 editText 中获取数据并显示在 textView 中? - How to get data from multiple editText and show in textView if one or more is empty in Android Studio? 使用按钮的Android数字时钟TextView TimePicker - Android digital clock textview timepicker using buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM