本文用一個 Demo 說明 Android 中 DatePickerDialog 與 TimePickerDialog 的用法:點擊【選擇日期】或【選擇時間】按鈕會彈出對話框,設定完成後,選取值會顯示在對應的文本框中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="歡迎關注 Andy.Chen Blog" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="日期和時間控件的使用 DEMO" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/showdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/pickdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇日期" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/showtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/picktime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇時間" />
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity {
private EditText showDate = null;
private Button pickDate = null;
private EditText showTime = null;
private Button pickTime = null;
private static final int SHOW_DATAPICK = 0;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_TIMEPICK = 2;
private static final int TIME_DIALOG_ID = 3;
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeViews();
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
setDateTime();
setTimeOfDay();
}
/** 初始化控制項與 UI 視圖 */
private void initializeViews(){
showDate = (EditText) findViewById(R.id.showdate);
pickDate = (Button) findViewById(R.id.pickdate);
showTime = (EditText) findViewById(R.id.showtime);
pickTime = (Button) findViewById(R.id.picktime);
pickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = new Message();
if (pickDate.equals((Button) v)) {
msg.what = MainActivity.SHOW_DATAPICK;
}
MainActivity.this.dateandtimeHandler.sendMessage(msg);
}
});
pickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = new Message();
if (pickTime.equals((Button) v)) {
msg.what = MainActivity.SHOW_TIMEPICK;
}
MainActivity.this.dateandtimeHandler.sendMessage(msg);
}
});
}
/** 設定日期 */
private void setDateTime(){
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDateDisplay();
}
/** 更新日期顯示 */
private void updateDateDisplay(){
showDate.setText(new StringBuilder().append(mYear).append("-")
.append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
.append((mDay < 10) ? "0" + mDay : mDay));
}
/** 日期控制項的事件 */
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDateDisplay();
}
};
/** 設定時間 */
private void setTimeOfDay(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateTimeDisplay();
}
/** 更新時間顯示 */
private void updateTimeDisplay(){
showTime.setText(new StringBuilder().append(mHour).append(":")
.append((mMinute < 10) ? "0" + mMinute : mMinute));
}
/** 時間控制項事件 */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateTimeDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
}
}
/** 處理日期和時間控制項的 Handler */
Handler dateandtimeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MainActivity.SHOW_DATAPICK:
showDialog(DATE_DIALOG_ID);
break;
case MainActivity.SHOW_TIMEPICK:
showDialog(TIME_DIALOG_ID);
break;
}
}
};
}
幾個實作重點:日期與時間各用一組常數區分 dialog id;透過 Handler 接收按鈕訊息再呼叫 showDialog,讓 Dialog 由 Activity 統一管理;onPrepareDialog 中呼叫 updateDate/updateTime,可以確保每次開啟對話框時顯示的都是最新的值。