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

Android 時間與時鐘元件實戰:TimePicker、DatePicker、AnalogClock、DigitalClock

編輯:關於 Android 程式設計

Picker 這個字在圖形介面裡就是「選擇器」的意思。Android 內建了四個跟時間、日期、時鐘有關的元件:

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

一、TimePicker

幾個關鍵 API:

  • 監聽器介面是 OnTimeChangedListener。
  • 預設顯示系統當前時間,可用 setCurrentHour() 與 setCurrentMinute() 改預設值。
  • 用 setIs24HourView(true) 切換成 24 小時制。
  • 讀取目前選定值用 getCurrentHour() 與 getCurrentMinute()。

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

真機 Android 2.3.7(非 24 小時制):

真機 Android 2.3.7(24 小時制):

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

與 TimePicker 不同,DatePicker 沒有對應 OnTimeChangedListener 那樣的即時監聽介面,只能透過「按下按鈕」這種方式去取值(要即時反應就得搭配 DatePickerDialog)。取值用 getYear()、getMonth()、getDayOfMonth() 三個方法。

模擬器 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);

        ButtonListener buttonListener = new ButtonListener();
        button.setOnClickListener(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 起始的月份,顯示給使用者看時記得 +1。

三、AnalogClock

指針時鐘顯示的時間會跟著系統時間走,不需要寫任何 Java 程式碼,只要在版面檔裡放一個標籤即可。

<AnalogClock />

模擬器 Android 4.2 效果圖:

真機 Android 2.3.7 效果圖:

四、DigitalClock

同樣會自動跟著系統時間更新,版面配置可以調整字色與字級:

<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>

模擬器 Android 4.2 效果圖:

真機 Android 2.3.7 效果圖:

一個實務上的提醒

AnalogClock 與 DigitalClock 在後續的 Android 版本中已被標記為不建議使用,官方提供的替代方案是 TextClock(API 17 起)。新專案若要顯示時鐘,建議直接用 TextClock 並透過 format12Hour/format24Hour 屬性控制格式;上面兩個元件則可以當成理解「系統時間驅動 UI 更新」的入門範例來看。

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