Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

A07:TimePicker、DatePicker、AnalogClock 與 DigitalClock 用法小結

編輯:關於 Android 程式設計

這篇的目標是釐清時間、日期與時鐘相關元件的設定方式。picker 這個字在原文裡就是「選擇器」的意思,四個元件各對應如下:

  • TimePicker——時間選擇器
  • DatePicker——日期選擇器
  • AnalogClock——指針式時鐘
  • DigitalClock——數字式時鐘

一、TimePicker

幾個重點整理:

  1. 對應的監聽介面是 OnTimeChangedListener。
  2. 預設顯示系統當前時間,可用 setCurrentHour 與 setCurrentMinute 改寫預設值。
  3. 呼叫 setIs24HourView(true) 可切換成 24 小時制。
  4. 讀取當前值用 getCurrentHour 與 getCurrentMinute。

模擬器 Android 4.2 的顯示效果(非 24 小時制):

真機 Android 2.3.7 的顯示效果(非 24 小時制):

真機 Android 2.3.7 的顯示效果(24 小時制):

對照這三張圖可以明顯看出,不同系統版本的原生外觀差異不小,新版在 4.x 之後走向了更簡潔的時鐘樣式,這也是為什麼正式專案通常會自訂樣式或改用 Material 元件。

Java 程式碼:

package com.haut.a07_timepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TimePicker timePicker;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timePicker = (TimePicker) findViewById(R.id.timePickerId);
        button = (Button) findViewById(R.id.buttonId);

        // 為 timePicker 建立監聽器
        TimePickerListener timeListener = new TimePickerListener();
        timePicker.setOnTimeChangedListener(timeListener);

        // 為 button 建立監聽器
        ButtonListener buttonListener = new ButtonListener();
        button.setOnClickListener(buttonListener);

        // 預設顯示當前時間,也可以手動指定
        timePicker.setCurrentHour(12);
        timePicker.setCurrentMinute(0);

        // 設定為 24 小時制
        timePicker.setIs24HourView(true);
    }

    class TimePickerListener implements OnTimeChangedListener {
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            String time = hourOfDay + "點:" + minute + "分";
            Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
        }
    }

    class ButtonListener implements OnClickListener {
        public void onClick(View v) {
            String time = timePicker.getCurrentHour() + "點:"
                    + timePicker.getCurrentMinute() + "分";
            Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

版面程式碼:

<RelativeLayout 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:background="@drawable/folwer1"
    tools:context=".MainActivity" >

    <TimePicker
        android:id="@+id/timePickerId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

    <Button
        android:id="@+id/buttonId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取設置時間"
        android:layout_below="@id/timePickerId"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

二、DatePicker

值得注意的是,DatePicker 沒有像 TimePicker 那樣現成的 OnTimeChangedListener 介面,變更的即時回呼得靠對話框版本,這部分我在下一篇再補充。

模擬器 Android 4.2 效果圖:

手機 Android 2.3.7 效果圖:

Java 程式碼——這裡只能透過按鈕主動讀取目前選定值:

package com.haut.a07_datepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

    private DatePicker datePicker;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datePicker = (DatePicker) findViewById(R.id.datePickerId);
        button = (Button) findViewById(R.id.buttonId);
        button.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements OnClickListener {
        public void onClick(View v) {
            String date = datePicker.getYear() + "年:" + datePicker.getMonth()
                    + "月:" + datePicker.getDayOfMonth() + "日";
            Toast.makeText(MainActivity.this, date, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

版面程式碼:

<RelativeLayout 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:background="@drawable/leaf"
    tools:context=".MainActivity" >

    <DatePicker
        android:id="@+id/datePickerId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

    <Button
        android:id="@+id/buttonId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取設置日期"
        android:layout_below="@id/datePickerId"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

這裡有個一定要記住的坑:getMonth() 回傳的是 0 起始的月份,也就是一月會得到 0;而 getDayOfMonth() 是從 1 開始。直接顯示時記得 +1,否則使用者看到的日期會整整差一個月。

三、AnalogClock

顯示的時間會跟著系統時間變化,程式碼非常簡單,版面裡放一個 <AnalogClock/> 標籤就結束了。

模擬器 Android 4.2 效果圖:

手機 Android 2.3.7 效果圖:

四、DigitalClock

同樣會隨系統時間即時更新,這裡示範了用 textColor 與 textSize 調整外觀:

模擬器 Android 4.2 效果圖:

手機 Android 2.3.7 效果圖:

版面程式碼:

<RelativeLayout 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:background="@drawable/folwer"
    tools:context=".MainActivity" >

    <DigitalClock
        android:id="@+id/digitalClockId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textColor="#ff0000"
        android:textSize="30sp" />

</RelativeLayout>

補充一句現況:AnalogClock 與 DigitalClock 兩者都已被標記為 deprecated,官方建議改用 TextClock(可透過 format12Hour、format24Hour 與 timeZone 屬性自訂格式)。若只是單純顯示時間,TextClock 是更合適的選擇。具體操作等實際用到時再逐一補充。

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