You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
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;
/**
* 日期时间选择对话框
* 封装DateTimePicker组件的对话框用于选择具体的日期和时间
* 核心功能:
* 1. 提供直观的日期时间选择界面
* 2. 支持12/24小时制显示
* 3. 实时预览选择结果
* 4. 提供回调接口返回选择结果
*
* 使用场景:
* - 便签提醒时间设置
* - 事件日程安排
*/
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);
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());
}
/**
* 设置时间显示模式
* @param is24HourView true:24小时制 false:12小时制
*/
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView;
mDateTimePicker.set24HourView(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 按钮标识
*/
public void onClick(DialogInterface arg0, int arg1) {
// 点击确定按钮时,通过回调返回选择的日期时间
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}