/* * 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 - 日期时间选择对话框 // 主要功能:封装DateTimePicker控件,提供完整的日期时间选择对话框界面 package net.micode.notes.ui; // ======================= 导入区域 ======================= // Java日期时间 import java.util.Calendar; // 日历类,用于日期时间计算 // 应用内部资源 import net.micode.notes.R; // 资源文件R类 // 应用内部组件 import net.micode.notes.ui.DateTimePicker; // 日期时间选择器控件 import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; // 日期时间变化监听器 // Android对话框相关 import android.app.AlertDialog; // 警告对话框基类 import android.content.Context; // 上下文 import android.content.DialogInterface; // 对话框接口 import android.content.DialogInterface.OnClickListener; // 对话框点击监听器 // Android日期时间相关 import android.text.format.DateFormat; // 日期格式化工具 import android.text.format.DateUtils; // 日期工具类 // ======================= 日期时间选择对话框 ======================= /** * DateTimePickerDialog - 日期时间选择对话框 * 继承自AlertDialog,包含DateTimePicker控件 * 功能:提供日期时间选择界面,包含确定/取消按钮 * 实现接口:OnClickListener(处理确定按钮点击) */ public class DateTimePickerDialog extends AlertDialog implements OnClickListener { // ======================= 成员变量 ======================= /** 当前选择的日期时间 - Calendar对象存储用户选择的时间 */ private Calendar mDate = Calendar.getInstance(); /** 24小时制标志 - true: 24小时制; false: 12小时制 */ private boolean mIs24HourView; /** 日期时间设置回调接口 - 当用户点击确定时回调 */ private OnDateTimeSetListener mOnDateTimeSetListener; /** 日期时间选择器控件 - 核心选择组件 */ private DateTimePicker mDateTimePicker; // ======================= 回调接口定义 ======================= /** * OnDateTimeSetListener - 日期时间设置监听器接口 * 当用户完成日期时间选择并点击确定按钮时回调 */ 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); // 1. 创建日期时间选择器控件 mDateTimePicker = new DateTimePicker(context); // 2. 将选择器控件设置为对话框的内容视图 setView(mDateTimePicker); // 3. 设置日期时间变化监听器 mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { /** * 日期时间变化回调 * 当用户在DateTimePicker上修改日期时间时触发 * @param view 触发变化的DateTimePicker控件 * @param year 年 * @param month 月(0-11) * @param dayOfMonth 日(1-31) * @param hourOfDay 小时(0-23) * @param minute 分钟(0-59) */ public void onDateTimeChanged(DateTimePicker view, int year, int month, int dayOfMonth, int hourOfDay, int minute) { // 更新内部Calendar对象 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()); } }); // 4. 设置初始日期时间 mDate.setTimeInMillis(date); // 设置时间戳 mDate.set(Calendar.SECOND, 0); // 秒数设为0(只精确到分钟) // 5. 更新日期时间选择器控件的当前值 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 6. 设置对话框按钮 // 确定按钮 - 点击后触发回调 setButton(context.getString(R.string.datetime_dialog_ok), this); // 取消按钮 - 点击后直接关闭对话框 setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); // 7. 设置24小时制模式(根据系统设置) set24HourView(DateFormat.is24HourFormat(this.getContext())); // 8. 更新对话框标题显示 updateTitle(mDate.getTimeInMillis()); } // ======================= 24小时制设置 ======================= /** * 设置24小时制显示模式 * @param is24HourView true: 24小时制; false: 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; // 注意:这里代码有bug,应该是 FORMAT_24HOUR 或 FORMAT_12HOUR // 格式化日期时间并设置为对话框标题 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); } // ======================= 按钮点击处理 ======================= /** * 对话框按钮点击回调 * 实现OnClickListener接口 * 当用户点击确定按钮时触发 * @param arg0 被点击的对话框 * @param arg1 被点击的按钮标识 * DialogInterface.BUTTON_POSITIVE: 确定按钮 * DialogInterface.BUTTON_NEGATIVE: 取消按钮 * DialogInterface.BUTTON_NEUTRAL: 中立按钮 */ public void onClick(DialogInterface arg0, int arg1) { // 只有确定按钮会触发此回调 if (mOnDateTimeSetListener != null) { // 触发日期时间设置回调 mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); } } }