/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * 文件: DateTimePickerDialog.java * 描述: 日期时间选择对话框 * 作用: 提供一个对话框界面,让用户可以选择日期和时间 * 功能: * 1. 基于AlertDialog,集成DateTimePicker控件 * 2. 提供确定和取消按钮 * 3. 自动更新对话框标题为当前选择的日期时间 * 4. 支持回调接口通知外部日期时间的设置 */ package net.micode.notes.ui; import java.util.Calendar; import net.micode.notes.R; import net.micode.notes.ui.DateTimePicker; import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.text.format.DateFormat; import android.text.format.DateUtils; /** * 日期时间选择对话框类 * * 基于AlertDialog,集成DateTimePicker控件,提供一个完整的日期时间选择界面。 * 用户可以通过此对话框设置日期和时间,并通过回调接口将选择结果通知给调用者。 * 对话框标题会自动更新为当前选择的日期时间的格式化字符串。 */ public class DateTimePickerDialog extends AlertDialog implements OnClickListener { /** 存储当前选择的日期时间 */ private Calendar mDate = Calendar.getInstance(); /** 是否使用24小时制显示 */ private boolean mIs24HourView; /** 日期时间设置监听器 */ private OnDateTimeSetListener mOnDateTimeSetListener; /** 日期时间选择器控件 */ private DateTimePicker mDateTimePicker; /** * 日期时间设置监听器接口 * 当用户点击对话框的确定按钮时,通过此接口通知调用者 */ public interface OnDateTimeSetListener { /** * 当日期时间被设置时调用 * * @param dialog 触发事件的对话框 * @param date 用户选择的日期时间(毫秒) */ void OnDateTimeSet(AlertDialog dialog, long date); } /** * 构造函数 * * @param context 上下文环境 * @param date 初始日期时间(毫秒) */ public DateTimePickerDialog(Context context, long date) { super(context); // 创建日期时间选择器控件 mDateTimePicker = new DateTimePicker(context); // 将选择器设置为对话框的内容视图 setView(mDateTimePicker); // 设置日期时间变更监听器 mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { public void onDateTimeChanged(DateTimePicker view, int year, int month, int dayOfMonth, int hourOfDay, int minute) { // 更新日期对象 mDate.set(Calendar.YEAR, year); mDate.set(Calendar.MONTH, month); mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); mDate.set(Calendar.MINUTE, minute); // 更新对话框标题为当前选择的日期时间 updateTitle(mDate.getTimeInMillis()); } }); // 设置初始日期时间 mDate.setTimeInMillis(date); // 将秒设置为0 mDate.set(Calendar.SECOND, 0); // 设置选择器的当前日期 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 设置确定按钮 setButton(context.getString(R.string.datetime_dialog_ok), this); // 设置取消按钮(不需要监听器,点击会自动关闭对话框) setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); // 根据系统设置决定是否使用24小时制 set24HourView(DateFormat.is24HourFormat(this.getContext())); // 更新对话框标题 updateTitle(mDate.getTimeInMillis()); } /** * 设置是否使用24小时制显示时间 * * @param is24HourView 如果为true,使用24小时制;否则使用12小时制 */ public void set24HourView(boolean is24HourView) { mIs24HourView = is24HourView; } /** * 设置日期时间设置监听器 * * @param callBack 监听器回调接口 */ public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { mOnDateTimeSetListener = callBack; } /** * 更新对话框标题为格式化的日期时间字符串 * * @param date 要显示的日期时间(毫秒) */ private void updateTitle(long date) { // 设置日期时间格式化标志 int flag = DateUtils.FORMAT_SHOW_YEAR | // 显示年份 DateUtils.FORMAT_SHOW_DATE | // 显示日期 DateUtils.FORMAT_SHOW_TIME; // 显示时间 // 根据是否使用24小时制设置相应的格式 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; // 设置对话框标题为格式化的日期时间字符串 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); } /** * 确定按钮点击事件处理 * * @param arg0 对话框接口 * @param arg1 按钮ID */ public void onClick(DialogInterface arg0, int arg1) { // 如果设置了监听器,通知日期时间已设置 if (mOnDateTimeSetListener != null) { mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); } } }