編輯:Android 技術基礎
這一節介紹幾個用來顯示時間的 Android 原生控件:TextClock、AnalogClock 和 Chronometer(至於已經過時的 DigitalClock 就不展開了)。開始吧!
TextClock 是 Android 4.2(API 17)之後推出的、用來替代 DigitalClock 的控件。
它能以字符串格式顯示當前日期和時間,因此推薦在 4.2 以後使用。
它提供兩種格式:一種在 24 小時制下顯示,一種在 12 小時制下顯示,大多數人用默認即可。
可以調用它提供的 is24HourModeEnabled() 方法查看系統是否在用 24 小時制。在 24 小時模式下:
它給我們提供了下面這些方法(對應也有 get 方法):
其實我們花費最多精力的地方在時間格式的定義上,也就是那個 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 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" />
運行結果:

顧名思義,就是一個簡單的計時器,直接上示例:
布局代碼:
<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 三個組件。從篇幅也能看出,它們實際用得並不多,用法卻超簡單,知道有這麼回事即可。本節就到這裡,謝謝~
Android 事件處理入門:以監聽器模型為核心的兩種寫法
任何以 UI 為核心的程式,都離不開事件處理。試想一個畫面:點按鈕沒反應、輸入框打完字不會回應,那就像一個人聽不到聲音一樣。Android 提供了兩條路——基於監聽器的事
Android 觸摸事件:OnTouchListener、OnTouchEvent 與多點觸碰
OnTouchListener 與 OnTouchEvent 的差異 Android 的觸摸處理有兩種常見方式:OnTouchListener 是監聽模式,通常附加在 V
TabHost 標籤頁實作:用分頁切換不同內容
TabHost 的作用類似桌面應用程式裡的「分頁」或「選項卡」:點一下標籤,下方內容就跟著換。Android 提供兩種實作路徑,一種是讓 Activity 繼承 TabA
Android 教學:RadioGroup 與 CheckBox 的 OnCheckedChangeListener 事件
單選按鈕 RadioGroup 和複選框 CheckBox 都各自有 OnCheckedChangeListener 事件,但兩者並不是同一個介面。本章用同一個畫面示範兩