Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

Date & Time 組件(上):TextClock、AnalogClock 與 Chronometer

編輯:Android 技術基礎

本節引言:

這一節介紹幾個用來顯示時間的 Android 原生控件:TextClock、AnalogClock 和 Chronometer(至於已經過時的 DigitalClock 就不展開了)。開始吧!


一、TextClock(文本時鐘)

TextClock 是 Android 4.2(API 17)之後推出的、用來替代 DigitalClock 的控件。
它能以字符串格式顯示當前日期和時間,因此推薦在 4.2 以後使用。
它提供兩種格式:一種在 24 小時制下顯示,一種在 12 小時制下顯示,大多數人用默認即可。

可以調用它提供的 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 哦!


二、AnalogClock(模擬時鐘)

就是下圖這種:

官網裡可以看到這樣三個屬性:

依次是:表背景、時針、分針的圖片,都可以自定義:

示例代碼如下:

 <AnalogClock android:layout_width="100dp" android:layout_height="100dp" android:dial="@mipmap/ic_c_bg" android:hand_hour="@mipmap/zhen_shi" android:hand_minute="@mipmap/zhen_fen" /> 

運行結果:


三、Chronometer(計時器)

顧名思義,就是一個簡單的計時器,直接上示例:

布局代碼:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Chronometer android:id="@+id/chronometer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#ff0000" android:textSize="60dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:orientation="horizontal"> <Button android:id="@+id/btnStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="開始記時" /> <Button android:id="@+id/btnStop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止記時" /> <Button android:id="@+id/btnReset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="重置" /> <Button android:id="@+id/btn_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="格式化" /> </LinearLayout> </LinearLayout> 

MainActivity.java

 public class MainActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{ private Chronometer chronometer; private Button btn_start,btn_stop,btn_base,btn_format; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { chronometer = (Chronometer) findViewById(R.id.chronometer); btn_start = (Button) findViewById(R.id.btnStart); btn_stop = (Button) findViewById(R.id.btnStop); btn_base = (Button) findViewById(R.id.btnReset); btn_format = (Button) findViewById(R.id.btn_format); chronometer.setOnChronometerTickListener(this); btn_start.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_base.setOnClickListener(this); btn_format.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnStart: chronometer.start();// 開始計時 break; case R.id.btnStop: chronometer.stop();// 停止計時 break; case R.id.btnReset: chronometer.setBase(SystemClock.elapsedRealtime());// 復位 break; case R.id.btn_format: chronometer.setFormat("Time:%s");// 更改時間顯示格式 break; } } @Override public void onChronometerTick(Chronometer chronometer) { String time = chronometer.getText().toString(); if(time.equals("00:00")){ Toast.makeText(MainActivity.this,"時間到了~",Toast.LENGTH_SHORT).show(); } } } 

運行截圖:


小結

這一節簡單介紹了 TextClock、AnalogClock、Chronometer 三個組件。從篇幅也能看出,它們實際用得並不多,用法卻超簡單,知道有這麼回事即可。本節就到這裡,謝謝~

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved