|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* 版权信息,表明这段代码由MiCode开源社区在2010-2011年间创作。
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
*
|
|
|
* 声明这段代码遵循Apache License 2.0版本。
|
|
|
*
|
|
|
* 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;
|
|
|
|
|
|
// 导入Java的Calendar类,用于处理日期和时间。
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
// 导入项目资源文件。
|
|
|
|
|
|
import net.micode.notes.ui.DateTimePicker;
|
|
|
// 导入自定义的DateTimePicker类。
|
|
|
|
|
|
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
|
|
|
// 导入DateTimePicker类的内部接口OnDateTimeChangedListener。
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
// 导入Android的AlertDialog类,用于显示对话框。
|
|
|
|
|
|
import android.content.Context;
|
|
|
// 导入Android的Context类,表示应用环境的全局信息。
|
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
// 导入Android的DialogInterface接口,表示对话框的接口。
|
|
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
|
|
// 导入DialogInterface接口中的OnClickListener内部接口,用于处理对话框按钮的点击事件。
|
|
|
|
|
|
import android.text.format.DateFormat;
|
|
|
// 导入Android的DateFormat类,用于格式化日期。
|
|
|
|
|
|
import android.text.format.DateUtils;
|
|
|
// 导入Android的DateUtils类,提供日期和时间的格式化方法。
|
|
|
|
|
|
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
|
|
// 声明DateTimePickerDialog类,它继承自AlertDialog并实现OnClickListener接口。
|
|
|
|
|
|
private Calendar mDate = Calendar.getInstance();
|
|
|
// 声明一个Calendar对象,用于存储日期和时间。
|
|
|
|
|
|
private boolean mIs24HourView;
|
|
|
// 声明一个布尔变量,用于指示是否使用24小时制显示时间。
|
|
|
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;
|
|
|
// 声明一个OnDateTimeSetListener接口类型的变量,用于回调日期时间设置完成的操作。
|
|
|
|
|
|
private DateTimePicker mDateTimePicker;
|
|
|
// 声明一个DateTimePicker对象,用于显示日期和时间选择器。
|
|
|
|
|
|
// 声明一个内部接口OnDateTimeSetListener,用于回调日期时间设置完成的操作。
|
|
|
public interface OnDateTimeSetListener {
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);
|
|
|
}
|
|
|
|
|
|
// DateTimePickerDialog的构造函数,初始化对话框。
|
|
|
public DateTimePickerDialog(Context context, long date) {
|
|
|
super(context);
|
|
|
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) {
|
|
|
// 当日期时间变化时更新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());
|
|
|
// 更新对话框标题。
|
|
|
}
|
|
|
});
|
|
|
mDate.setTimeInMillis(date);
|
|
|
// 设置Calendar对象的初始时间为传入的日期。
|
|
|
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_12HOUR;
|
|
|
// 根据是否使用24小时制设置格式标志。
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
// 使用DateUtils格式化日期时间并设置为对话框标题。
|
|
|
}
|
|
|
|
|
|
// 处理对话框按钮的点击事件。
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
// 如果设置了日期时间设置完成的回调监听器,则调用其OnDateTimeSet方法。
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |