You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git.text/src/ui/DateTimePickerDialog.java

137 lines
5.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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内部集成DateTimePicker作为选择视图提供日期和时间的可视化选择
*/
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
// 用于维护当前选中的日期时间
private Calendar mDate = Calendar.getInstance();
// 是否采用24小时制显示时间
private boolean mIs24HourView;
// 日期时间选择完成后的回调监听器由调用者实现如NoteEditActivity
private OnDateTimeSetListener mOnDateTimeSetListener;
// 自定义的日期时间选择器视图,负责用户选择交互
private DateTimePicker mDateTimePicker;
/**
* 日期时间选择完成的回调接口
* 当用户点击"确定"后,通过此接口将选中的时间(毫秒级时间戳)传递给调用者
*/
public interface OnDateTimeSetListener {
void OnDateTimeSet(AlertDialog dialog, long date);
}
/**
* 构造方法:初始化日期时间选择对话框
* @param context 上下文环境如NoteEditActivity
* @param 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);
// 根据新选择的时间更新对话框标题
updateTitle(mDate.getTimeInMillis());
}
});
// 设置初始日期时间(忽略秒数,确保提醒时间精确到分钟)
mDate.setTimeInMillis(date);
mDate.set(Calendar.SECOND, 0);
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
// 设置对话框按钮:"确定"按钮触发onClick方法"取消"按钮不执行操作
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小时制/12小时制
* @param is24HourView true为24小时制false为12小时制
*/
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView;
}
/**
* 设置日期时间选择完成后的回调监听器
* @param callBack 实现了OnDateTimeSetListener的对象通常为调用对话框的Activity
*/
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;
// 根据24小时制模式调整显示格式
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR;
// 格式化时间并设置为对话框标题
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
/**
* 处理"确定"按钮的点击事件
* 当用户点击确定后,通过回调接口将选中的时间传递给调用者
*/
public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener != null) {
// 回调传递选中的时间(毫秒级时间戳)
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}