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.
demo/Notes-master/src/net/micode/notes/ui/DateTimePickerDialog.java

157 lines
6.2 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;
/**
* DateTimePickerDialog 类继承自 AlertDialog用于显示一个日期和时间选择对话框。
* 用户可以通过这个对话框选择特定的日期和时间。
*/
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
// 用于存储用户选择的日期和时间的 Calendar 对象
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);
}
/**
* 构造函数,用于创建一个 DateTimePickerDialog 实例。
* @param context 上下文对象
* @param date 初始显示的日期和时间的毫秒数
*/
public DateTimePickerDialog(Context context, long date) {
super(context);
// 创建一个 DateTimePicker 实例
mDateTimePicker = new DateTimePicker(context);
// 将 DateTimePicker 设置为对话框的视图
setView(mDateTimePicker);
// 为 DateTimePicker 设置日期和时间改变监听器
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
/**
* 当日期或时间发生改变时调用此方法。
* @param view 日期和时间选择器视图
* @param year 选择的年份
* @param month 选择的月份
* @param dayOfMonth 选择的日期
* @param hourOfDay 选择的小时
* @param minute 选择的分钟
*/
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());
}
});
// 设置 Calendar 对象的时间为传入的初始时间
mDate.setTimeInMillis(date);
// 将秒数设置为 0
mDate.set(Calendar.SECOND, 0);
// 设置 DateTimePicker 的当前日期和时间
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 是否使用 24 小时制
*/
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView;
}
/**
* 设置日期和时间设置完成后的回调监听器。
* @param callBack 实现了 OnDateTimeSetListener 接口的回调对象
*/
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;
// 使用 DateUtils 格式化日期和时间,并设置为对话框的标题
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
/**
* 处理对话框按钮的点击事件。
* @param arg0 对话框接口对象
* @param arg1 按钮的索引
*/
public void onClick(DialogInterface arg0, int arg1) {
// 如果设置了回调监听器,则调用其 OnDateTimeSet 方法
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}