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.
xiaomi-Notes/DateTimePickerDialog.java

118 lines
5.3 KiB

/*
* 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; // 导入日期工具类,提供日期格式化和显示功能
// 自定义日期时间选择对话框类
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
private Calendar mDate = Calendar.getInstance();
// 用于存储当前选中的日期和时间
private boolean mIs24HourView;
// 标识是否使用24小时制
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); // 将秒数设为0
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
// 设置选择器的当前日期时间
setButton(context.getString(R.string.datetime_dialog_ok), this);
// 设置“确定”按钮
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
// 设置“取消”按钮
set24HourView(DateFormat.is24HourFormat(this.getContext()));
// 根据系统设置决定是否使用24小时制
updateTitle(mDate.getTimeInMillis());
// 初始化对话框标题
}
// 设置是否使用24小时制
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView; // 更新24小时制标识
}
// 设置日期时间设置的监听器
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack; // 保存监听器
}
// 更新对话框的标题,显示当前选中的日期和时间
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_12HOUR;
// 根据设置选择小时制
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
// 格式化日期时间并设置为对话框标题
}
// 点击事件处理
public void onClick(DialogInterface arg0, int arg1) {
// 如果设置了监听器,则调用其方法并传递当前时间戳
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}