|
|
|
@ -0,0 +1,98 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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; // 引入Calendar类用于处理日期和时间
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R; // 引入R文件,用于获取资源ID
|
|
|
|
|
import net.micode.notes.ui.DateTimePicker; // 引入自定义的DateTimePicker控件
|
|
|
|
|
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; // 引入DateTimePicker控件的监听器
|
|
|
|
|
|
|
|
|
|
import android.app.AlertDialog; // 引入AlertDialog类,用于创建对话框
|
|
|
|
|
import android.content.Context; // 引入Context类,用于访问应用程序上下文
|
|
|
|
|
import android.content.DialogInterface; // 引入DialogInterface类,用于处理对话框的按钮事件
|
|
|
|
|
import android.content.DialogInterface.OnClickListener; // 引入OnClickListener接口,用于按钮点击事件
|
|
|
|
|
import android.text.format.DateFormat; // 引入DateFormat类,用于格式化日期和时间
|
|
|
|
|
import android.text.format.DateUtils; // 引入DateUtils类,用于时间格式化与操作
|
|
|
|
|
|
|
|
|
|
// 定义一个自定义的DateTimePickerDialog类,继承自AlertDialog
|
|
|
|
|
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); // 调用父类构造函数,初始化AlertDialog
|
|
|
|
|
mDateTimePicker = new DateTimePicker(context); // 初始化DateTimePicker控件
|
|
|
|
|
setView(mDateTimePicker); // 设置对话框的内容视图为DateTimePicker控件
|
|
|
|
|
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()); // 设置DateTimePicker控件的初始日期
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置日期时间设置完成后的回调监听器
|
|
|
|
|
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_24HOUR; // 如果是24小时制,显示24小时格式
|
|
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); // 格式化日期时间并设置为对话框标题
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确认按钮点击事件的处理
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
|
|
// 调用回调接口,传递设置的日期时间
|
|
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|