編輯:Android 技術基礎
要讓使用者挑一個日期或時刻,最糟的做法是丟一個空白輸入框讓他手打「2026-08-26」。Android 內建的日期與時間對話框,會自動依系統地區給出對應的日曆與時鐘介面,是既省事又不容易出錯的選擇。

開啟 res/layout/activity_main.xml。這裡有一個小技巧:把顯示用的 EditText 設為不可編輯、隱藏游標,讓它只當「結果顯示區」,避免使用者繞過對話框直接打字。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false" />
<Button
android:id="@+id/dateBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="日期對話框" />
<Button
android:id="@+id/timeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="時間對話框" />
<DigitalClock
android:id="@+id/DigitalClock01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dip" />
<AnalogClock
android:id="@+id/analogClock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
版面下方還放了 DigitalClock 與 AnalogClock 兩個時鐘元件,方便你比對「挑選的時間」和「目前的時間」。
開啟 src/com.genwoxue.datetimedialog/MainActivity.java:
package com.genwoxue.datetimedialog;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private Button dateBtn = null;
private Button timeBtn = null;
private EditText et = null;
private static final int DATE_DIALOG = 0;
private static final int TIME_DIALOG = 1;
private Calendar c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
dateBtn = (Button) findViewById(R.id.dateBtn);
timeBtn = (Button) findViewById(R.id.timeBtn);
dateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG);
}
});
timeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG);
}
});
}
/** 建立日期與時間選擇對話框 */
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DATE_DIALOG:
c = Calendar.getInstance();
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
// 注意:month 從 0 開始,顯示時要 +1
et.setText("您選擇了:" + year + "年" + (month + 1) + "月" + dayOfMonth + "日");
}
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
break;
case TIME_DIALOG:
c = Calendar.getInstance();
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et.setText("您選擇了:" + hourOfDay + "時" + minute + "分");
}
},
c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
false); // false = 12 小時制;true = 24 小時制
break;
}
return dialog;
}
}
月份從 0 開始算。 Calendar.MONTH 的回傳值 0 代表一月,所以顯示給使用者看之前必須 +1,這是新手最常出現「日期少一個月」的原因。
最後一個參數決定時制。 TimePickerDialog 的最後一個 boolean 傳 false 走 12 小時制、true 走 24 小時制。專案若有固定格式需求,明確指定比依賴系統設定更可靠。
另外,showDialog() 搭配 onCreateDialog() 是舊寫法:系統會幫你快取對話框、並在 Activity 重建時自動還原狀態。新專案更常直接 new DatePickerDialog(...).show(),程式碼更直觀,但旋轉螢幕後的狀態就得自己處理。




Android Service 入門:線程概念與生命週期
線程與 Service 的關係 學習 Service 前,先釐清幾個常見概念:程式是靜態的指令集合,進程是執行中的程式,線程則是進程中的執行單元。Service 是 An
3.2 回呼式(Callback)事件處理機制詳解
前言 3.1 我們談的是「監聽式」事件處理:替事件來源(元件)掛一個監聽器,使用者一操作,事件就交給監聽器去處理,不同事件走不同分支。那麼回呼式(callback)又是
2.6.1 PopupWindow 懸浮視窗基本用法
前言 本節介紹的是最後一個用於顯示資訊的 UI 元件——PopupWindow(懸浮視窗)。想知道它長什麼樣?打開手機裡的 QQ,長按清單上的某一項,這時會彈出一個黑色
Android Service 進階:Binder 與 AIDL 跨進程通信
Binder 是什麼 Binder 是 Android 中重要的跨進程通信基礎。它讓不同進程之間可以像呼叫本地方法一樣互動,而開發者通常不需要直接實作底層細節,只要依照