如何使用 Kotlin 在 android 中更改 TextClock 的格式
How to change format of TextClock in android with Kotlin
<TextClock
android:layout_marginTop="20dp"
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="45dp"
android:format12Hour="hh:mm:ss a"
android:layout_gravity="center"/>
这是我布局文件中的 TextClock
我想以编程方式将其格式从 12 小时更改为 24 小时
到目前为止我试过这个:
var textClock: TextClock = view.findViewById<TextClock>(R.id.textClock)
textClock.format24Hour = "hh:mm:ss a"
但似乎没有任何效果。
在 java 中有一个名为 setFormat24Hour(CharSequence format)
的方法,但在 Kotlin 中我无法达到预期的结果。
我们将不胜感激您的帮助。谢谢
如documentation所说,setFormat24Hour(CharSequence)
方法:
Specifies the formatting pattern used to display the date and/or time in 24-hour mode.
它实际上并没有将时钟更改为 24 小时制。相反,TextClock
使用系统默认设置来决定使用哪种格式。
但是如果你真的想强制格式,你应该将其他模式的格式设置为 null
,这将强制它显示你想要的。
<TextClock
android:layout_marginTop="20dp"
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="45dp"
android:format12Hour="hh:mm:ss a"
android:layout_gravity="center"/>
这是我布局文件中的 TextClock 我想以编程方式将其格式从 12 小时更改为 24 小时
到目前为止我试过这个:
var textClock: TextClock = view.findViewById<TextClock>(R.id.textClock)
textClock.format24Hour = "hh:mm:ss a"
但似乎没有任何效果。
在 java 中有一个名为 setFormat24Hour(CharSequence format)
的方法,但在 Kotlin 中我无法达到预期的结果。
我们将不胜感激您的帮助。谢谢
如documentation所说,setFormat24Hour(CharSequence)
方法:
Specifies the formatting pattern used to display the date and/or time in 24-hour mode.
它实际上并没有将时钟更改为 24 小时制。相反,TextClock
使用系统默认设置来决定使用哪种格式。
但是如果你真的想强制格式,你应该将其他模式的格式设置为 null
,这将强制它显示你想要的。