|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
/**
|
|
|
* 继承自 AlertDialog 的类,用于显示一个包含日期和时间选择器的对话框。
|
|
|
*/
|
|
|
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
|
|
|
|
|
/**
|
|
|
* 用于存储当前日期的 Calendar 对象。
|
|
|
*/
|
|
|
private Calendar mDate = Calendar.getInstance();
|
|
|
|
|
|
/**
|
|
|
* 表示是否使用 24 小时视图的布尔标志。
|
|
|
*/
|
|
|
private boolean mIs24HourView;
|
|
|
|
|
|
/**
|
|
|
* 当日期和时间被设置时调用的监听器。
|
|
|
*/
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;
|
|
|
|
|
|
/**
|
|
|
* 用于显示日期和时间的 DateTimePicker 实例。
|
|
|
*/
|
|
|
private DateTimePicker mDateTimePicker;
|
|
|
|
|
|
/**
|
|
|
* 定义当日期和时间被设置时的回调接口。
|
|
|
*/
|
|
|
public interface OnDateTimeSetListener {
|
|
|
/**
|
|
|
* 当日期和时间被设置时调用的方法。
|
|
|
* @param dialog 显示日期和时间的对话框。
|
|
|
* @param date 当前日期的毫秒时间戳。
|
|
|
*/
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构造方法,创建一个初始日期为指定 long 类型日期时间的日期时间选择器对话框。
|
|
|
* @param context 上下文环境。
|
|
|
* @param date 初始日期时间,以毫秒为单位。
|
|
|
*/
|
|
|
public DateTimePickerDialog(Context context, long date) {
|
|
|
super(context);
|
|
|
mDateTimePicker = new DateTimePicker(context);
|
|
|
setView(mDateTimePicker);
|
|
|
setButton(context.getString(R.string.datetime_dialog_ok), this);
|
|
|
setButton2(context.getString(R.string.datetime_dialog_cancel), null);
|
|
|
set24HourView(DateFormat.is24HourFormat(context));
|
|
|
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
|
|
|
@Override
|
|
|
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());
|
|
|
updateTitle(mDate.getTimeInMillis());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置是否使用 24 小时视图。
|
|
|
* @param is24HourView 如果为 true,则使用 24 小时视图;如果为 false,则使用 12 小时视图。
|
|
|
*/
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
mIs24HourView = is24HourView;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置当日期和时间被设置时调用的监听器。
|
|
|
* @param callBack 日期和时间设置的回调监听器。
|
|
|
*/
|
|
|
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
|
|
|
mOnDateTimeSetListener = callBack;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据指定的日期更新对话框的标题。
|
|
|
* @param date 指定日期的毫秒时间戳。
|
|
|
*/
|
|
|
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;
|
|
|
setTitle(DateUtils.formatDateTime(getContext(), date, flag));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 当点击对话框中的按钮时调用。
|
|
|
* @param dialogInterface 对话框界面。
|
|
|
* @param whichButton 按钮的索引。
|
|
|
*/
|
|
|
public void onClick(DialogInterface dialogInterface, int whichButton) {
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}
|
|
|
} |