|
|
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,用于显示一个包含日期时间选择器的对话框
|
|
|
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 {
|
|
|
void OnDateTimeSet(AlertDialog dialog, long 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);
|
|
|
// 设置对话框的取消按钮和点击监听器(这里传入null表示使用默认行为)
|
|
|
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener) null);
|
|
|
// 设置对话框的24小时制视图状态
|
|
|
set24HourView(DateFormat.is24HourFormat(this.getContext()));
|
|
|
// 更新对话框标题
|
|
|
updateTitle(mDate.getTimeInMillis());
|
|
|
}
|
|
|
|
|
|
// 设置对话框的24小时制视图状态
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
mIs24HourView = is24HourView;
|
|
|
}
|
|
|
|
|
|
// 设置日期时间设置监听器
|
|
|
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
|
|
mOnDateTimeSetListener = callBack;
|
|
|
}
|
|
|
|
|
|
// 更新对话框标题,根据传入的日期和当前的24小时制视图状态格式化日期时间字符串
|
|
|
private void updateTitle(long date) {
|
|
|
int flag =
|
|
|
DateUtils.FORMAT_SHOW_YEAR |
|
|
|
DateUtils.FORMAT_SHOW_DATE |
|
|
|
DateUtils.FORMAT_SHOW_TIME;
|
|
|
flag |= mIs24HourView? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
}
|
|
|
|
|
|
// 确定按钮点击事件处理,当点击确定按钮时,如果设置了日期时间设置监听器,则触发监听器并传递当前日期时间
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
if (mOnDateTimeSetListener!= null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}
|
|
|
} |