一、前言
TextClock可以将当前日期和/或时间显示为格式化字符串。
二、效果图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/teal_200" android:orientation="vertical" android:gravity="center" android:padding="8dp" tools:context=".MainActivity"> <TextClock android:id="@+id/timeView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center" android:layout_toStartOf="@+id/dataView" android:format24Hour ="HH:mm" android:format12Hour ="hh:mm" android:textColor="@color/white" android:textSize="50sp" /> <TextClock android:id="@+id/dataView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="12dp" android:format24Hour ="yyyy.MM.dd\nEE a" android:format12Hour ="yyyy.MM.dd\nEE a" android:textColor="@color/white" android:textSize="20sp"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_margin="@dimen/dimen_20" android:layout_height="match_parent"> <TextView android:id="@+id/tv_is24HourModeEnabled" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_10" android:textSize="@dimen/text_size_16" android:padding="@dimen/dimen_10"/> <TextClock android:id="@+id/tc_timeText_12" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black" android:textSize="30sp" android:textStyle="bold"/> <TextClock android:id="@+id/tc_dateText_12" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black" android:textSize="20sp"/> <TextView android:id="@+id/tv_12hour" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_10" android:textSize="@dimen/text_size_16" android:padding="@dimen/dimen_10"/> <TextClock android:id="@+id/tc_timeText_24" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_20" android:format12Hour="hh:mm:ss" android:format24Hour="HH:mm:ss" android:gravity="center" android:textColor="@color/black" android:textSize="30sp" android:textStyle="bold"/> <TextClock android:id="@+id/tc_dateText_24" android:layout_width="match_parent" android:layout_height="wrap_content" android:format12Hour="yyyy/MM/dd E" android:format24Hour="yyyy/MM/dd E" android:gravity="center" android:textColor="@color/black" android:textSize="20sp"/> <TextView android:id="@+id/tv_24hour" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/dimen_10" android:textSize="@dimen/text_size_16" android:padding="@dimen/dimen_10"/> private TextClock tc_timeText_12,tc_dateText_12,tc_timeText_24,tc_dateText_24; private TextView tv_12hour,tv_24hour,tv_is24HourModeEnabled; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_textview_textclock);//加载布局文件 initView(); } private void initView() { tv_is24HourModeEnabled = findViewById(R.id.tv_is24HourModeEnabled); tc_timeText_12 = findViewById(R.id.tc_timeText_12); tc_dateText_12 = findViewById(R.id.tc_dateText_12); tv_12hour = findViewById(R.id.tv_12hour); //setTimeZone使用(UTC-7)无效, //原因:源码未对UTC+(-)进行处理,下面有具体的源码分析 tc_timeText_12.setTimeZone("America/Los_Angeles");//有效 tc_dateText_12.setTimeZone("America/Los_Angeles");//有效 //tc_timeText_12.setTimeZone("GMT+7:00");//有效 //tc_dateText_12.setTimeZone("GMT+7:00");//有效 tc_dateText_12.setFormat24Hour("HH:mm"); tc_dateText_12.setFormat12Hour("yyyy/MM/dd E"); // EEEE:星期五 ;E/EE/EEE:周五;a:上午/下午 tc_dateText_12.setFormat24Hour("yyyy年MM月dd日 EEEE aa HH:mm:ss"); String format12 = "\n12小时模式格式:"+tc_timeText_12.getFormat12Hour(); format12 = format12+"\n24小时模式格式:"+tc_timeText_12.getFormat24Hour(); format12 = format12+"\n时区:"+tc_timeText_12.getTimeZone(); tv_12hour.setText("Format:"+format12); tc_timeText_24 = findViewById(R.id.tc_timeText_24); tc_dateText_24 = findViewById(R.id.tc_dateText_24); tv_24hour = findViewById(R.id.tv_24hour); String format = "\n24小时模式格式:"+tc_timeText_24.getFormat24Hour(); format = format+"\n12小时模式格式:"+tc_timeText_24.getFormat12Hour(); format = format+"\n时区:"+tc_timeText_24.getTimeZone(); String timeZome =TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT); format = format+"\n时区:"+timeZome; tv_24hour.setText("Format:"+format); String is24HourMode = String.format("系统当前是否使用 24 小时模式:%s。", tc_dateText_24.is24HourModeEnabled()); tv_is24HourModeEnabled.setText(is24HourMode); } }