0、介紹
TextClock是在Android 4.2(API 17)后推出的用來替代DigitalClock的一個控件!
TextClock可以以字符串格式顯示當前的日期和時間,因此推薦在Android 4.2以后使用TextClock。
這個控件推薦在24進制的android系統中使用,TextClock提供了兩種不同的格式, 一種是在24進制中顯示時間和日期,另一種是在12進制中顯示時間和日期。大部分人喜歡默認的設置。
可以通過調用:TextClock提供的is24HourModeEnabled()方法來查看,系統是否在使用24進制時間顯示! 在24進制模式中:
- 如果沒獲取時間,首先通過getFormat24Hour()返回值;
- 獲取失敗則通過getFormat12Hour()獲取返回值;
- 以上都獲取失敗則使用默認;
另外他給我們提供了下面這些方法,對應的還有get方法:
| Attribute Name | Related Method | Description |
|---|---|---|
| android:format12Hour | setFormat12Hour(CharSequence) | 設置12時制的格式 |
| android:format24Hour | setFormat24Hour(CharSequence) | 設置24時制的格式 |
| android:timeZone | setTimeZone(String) | 設置時區 |
其實更多的時間我們是花在時間形式定義上,就是里面這個CharSequence! 這里提供下常用的寫法以及結果:
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MM/dd/yy h:mmaa"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MMM dd, yyyy h:mmaa"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MMMM dd, yyyy h:mmaa"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="E, MMMM dd, yyyy h:mmaa"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="Noteworthy day: 'M/d/yy"/>
運行結果:

PS:另外minsdk 要大於或者等於17哦!
PS:
另外minsdk 要大於或者等於17哦!
1、布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"><TextClockandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:format24Hour="當前日期是yyyy年MMMMdd ,今天是EEEE ,當前時間是aa h:mm"/></LinearLayout>
2、java文件
package lpc.com.drawlayoutdemo1;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;publicclassMainActivityextendsAppCompatActivity{@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
3、效果
