|
|
/*
|
|
|
* 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); // 将 mDate 对象设置为新的日期和时间
|
|
|
updateTitle(mDate.getTimeInMillis()); //更新选择器的标题
|
|
|
}
|
|
|
});// 为 mDateTimePicker 设置一个匿名内部类作为日期时间变化监听器
|
|
|
|
|
|
mDate.setTimeInMillis(date);
|
|
|
mDate.set(Calendar.SECOND, 0);
|
|
|
// 将传入的时间戳设置为 mDate 对象的时间,并将秒数设置为零
|
|
|
|
|
|
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());// 更新选择器的标题
|
|
|
}
|
|
|
|
|
|
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_24HOUR;
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
// 格式化时间戳为指定的日期、时间和小时制格式的字符串,并设置为选择器的标题
|
|
|
}
|
|
|
//更新选择器的标题
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
// 如果日期时间设置监听器不为 null,则调用 OnDateTimeSet 方法并传递当前日期时间和对话框对象
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}//当单击对话框的按钮时被调用
|
|
|
|
|
|
} |