|
|
/*
|
|
|
* 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 {// 创建一个日期时间选择器对话框类,继承自AlertDialog类,并实现OnClickListener接口。
|
|
|
|
|
|
private Calendar mDate = Calendar.getInstance();// 创建一个日期时间选择器对话框类,继承自AlertDialog类,并实现OnClickListener接口。
|
|
|
private boolean mIs24HourView;// 创建一个日期时间选择器对话框类,继承自AlertDialog类,并实现OnClickListener接口。
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;// 创建一个日期时间选择器对话框类,继承自AlertDialog类,并实现OnClickListener接口。
|
|
|
private DateTimePicker mDateTimePicker;// 创建一个日期时间选择器对话框类,继承自AlertDialog类,并实现OnClickListener接口。
|
|
|
|
|
|
public interface OnDateTimeSetListener {// 创建一个日期时间设置监听器接口OnDateTimeSetListener。
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);// 创建一个日期时间设置监听器接口OnDateTimeSetListener。
|
|
|
}
|
|
|
|
|
|
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小时制或12小时制
|
|
|
updateTitle(mDate.getTimeInMillis());// 更新对话框标题,显示当前选择的时间
|
|
|
}
|
|
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
mIs24HourView = is24HourView; //将传入的值赋值给mIs24HourView变量
|
|
|
} //设置是否是24小时制的方法,传入一个boolean值
|
|
|
|
|
|
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {//设置日期时间设置监听器的方法,传入一个OnDateTimeSetListener对象
|
|
|
mOnDateTimeSetListener = callBack; //设置日期时间设置监听器的方法,传入一个OnDateTimeSetListener对象
|
|
|
}
|
|
|
|
|
|
private void updateTitle(long date) {//更新对话框标题的方法,传入一个日期时间的long型值
|
|
|
int flag =//更新对话框标题的方法,传入一个日期时间的long型值
|
|
|
DateUtils.FORMAT_SHOW_YEAR | //显示年份
|
|
|
DateUtils.FORMAT_SHOW_DATE | //显示日期
|
|
|
DateUtils.FORMAT_SHOW_TIME; //显示时间
|
|
|
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;//根据mIs24HourView变量的值,判断是否显示24小时制,将标志位赋值给flag变量
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));//根据传入的日期时间值和标志位,格式化日期时间并设置为对话框标题
|
|
|
}
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());//当用户点击对话框上的“确认”按钮时,如果设置了日期时间设置监听器,则调用该监听器的OnDateTimeSet方法
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |