|
|
/*
|
|
|
* 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 {
|
|
|
|
|
|
// 定义一个Calendar对象,用于存储日期和时间
|
|
|
private Calendar mDate = Calendar.getInstance();
|
|
|
// 定义一个布尔变量,用于判断是否为24小时制
|
|
|
private boolean mIs24HourView;
|
|
|
// 定义一个OnDateTimeSetListener对象,用于监听日期时间的设置
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;
|
|
|
// 定义一个DateTimePicker对象,用于显示日期时间选择器
|
|
|
private DateTimePicker mDateTimePicker;
|
|
|
|
|
|
// 定义一个接口,用于监听日期时间设置
|
|
|
public interface OnDateTimeSetListener {
|
|
|
// 当日期时间设置时,调用此方法
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);
|
|
|
}
|
|
|
|
|
|
// 定义一个DateTimePickerDialog类,传入Context和long类型的date参数
|
|
|
public DateTimePickerDialog(Context context, long date) {
|
|
|
// 调用父类的构造方法
|
|
|
super(context);
|
|
|
// 创建一个DateTimePicker对象
|
|
|
mDateTimePicker = new DateTimePicker(context);
|
|
|
// 设置视图
|
|
|
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);
|
|
|
// 设置当前日期
|
|
|
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
|
|
|
// 设置按钮
|
|
|
setButton(context.getString(R.string.datetime_dialog_ok), this);
|
|
|
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
|
|
|
// 设置24小时制
|
|
|
set24HourView(DateFormat.is24HourFormat(this.getContext()));
|
|
|
// 更新标题
|
|
|
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;
|
|
|
// 如果是24小时制,则设置格式为24小时制
|
|
|
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
|
|
|
// 格式化日期并设置标题
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
}
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
// 如果mOnDateTimeSetListener不为空,则调用其OnDateTimeSet方法,传入当前对象和mDate的时间戳
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |