|
|
/*
|
|
|
* 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类
|
|
|
* 并实现了OnClickListener接口
|
|
|
* 主要功能是创建一个日期和时间选择器对话框,用于让用户选择日期和时间
|
|
|
*/
|
|
|
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
|
|
//声明了一个Calendar对象mDate,用于存储选择的日期和时间。
|
|
|
private Calendar mDate = Calendar.getInstance();
|
|
|
//声明了一个boolean类型的变量mIs24HourView,用于表示是否使用24小时制。
|
|
|
private boolean mIs24HourView;
|
|
|
//声明了一个OnDateTimeSetListener接口,用于监听日期和时间的设置。
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;
|
|
|
//声明了一个DateTimePicker对象mDateTimePicker,用于显示日期和时间选择器。
|
|
|
private DateTimePicker mDateTimePicker;
|
|
|
//功能:定义了一个OnDateTimeSetListener接口,其中包含一个OnDateTimeSet()方法,用于在日期和时间被设置时回调。
|
|
|
|
|
|
public interface OnDateTimeSetListener {
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);
|
|
|
}
|
|
|
//功能:DateTimePickerDialog的构造函数,接收一个Context对象和一个long类型的date参数。
|
|
|
public DateTimePickerDialog(Context context, long date) {
|
|
|
//调用父类AlertDialog的构造函数。
|
|
|
super(context);
|
|
|
//创建一个DateTimePicker对象,并设置为对话框的视图。
|
|
|
mDateTimePicker = new DateTimePicker(context);
|
|
|
setView(mDateTimePicker);
|
|
|
//设置DateTimePicker的OnDateTimeChangedListener,当日期和时间发生改变时会回调onDateTimeChanged()方法。
|
|
|
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
|
|
|
//将给定的date参数设置给mDate,同时将秒数设置为0,并将当前的日期和时间设为DateTimePicker的当前选中值。
|
|
|
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);
|
|
|
//根据系统设置的时间制式,设置是否使用24小时制。
|
|
|
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()));
|
|
|
|
|
|
updateTitle(mDate.getTimeInMillis());
|
|
|
}
|
|
|
//设置是否使用24小时制。
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
mIs24HourView = is24HourView;
|
|
|
}
|
|
|
//设置一个OnDateTimeSetListener对象,用于监听日期和时间的设置。
|
|
|
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
|
|
mOnDateTimeSetListener = callBack;
|
|
|
}
|
|
|
//定义了一个私有方法updateTitle(),用于更新对话框的标题,根据给定的日期和时间。
|
|
|
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;
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
}
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |