TextClock的基本使用


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、布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_height="match_parent"
  4. android:layout_width="match_parent">
  5. <TextClock
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:format24Hour="當前日期是yyyy年MMMMdd ,今天是EEEE ,當前時間是aa h:mm"/>
  9. </LinearLayout>
 
2、java文件
  1. package lpc.com.drawlayoutdemo1;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. publicclassMainActivityextendsAppCompatActivity{
  5. @Override
  6. protectedvoid onCreate(Bundle savedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. }
  10. }
3、效果






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM