|
|
|
@ -1,102 +1,120 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
* 版权所有 (c) 2010-2011, MiCode 开源社区 (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
|
|
|
|
|
* 本文件授权使用 Apache License, Version 2.0(以下简称“许可证”);
|
|
|
|
|
* 除非符合许可证规定,否则不得使用此文件。
|
|
|
|
|
* 您可以从以下网址获取许可证副本:
|
|
|
|
|
*
|
|
|
|
|
* 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();
|
|
|
|
|
//创建一个Calendar类型的变量 mDate,方便时间的操作
|
|
|
|
|
private boolean mIs24HourView;
|
|
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener;
|
|
|
|
|
//声明一个时间日期滚动选择控件 mOnDateTimeSetListener
|
|
|
|
|
private DateTimePicker mDateTimePicker;
|
|
|
|
|
//DateTimePicker控件,控件一般用于让用户可以从日期列表中选择单个值。
|
|
|
|
|
//运行时,单击控件边上的下拉箭头,会显示为两个部分:一个下拉列表,一个用于选择日期的
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
//将视图中的各选项设置为系统当前时间
|
|
|
|
|
updateTitle(mDate.getTimeInMillis());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mDate.setTimeInMillis(date);
|
|
|
|
|
//得到系统时间
|
|
|
|
|
mDate.set(Calendar.SECOND, 0);
|
|
|
|
|
//将秒数设置为0
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
|
|
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
|
|
|
|
|
}//android开发中常见日期管理工具类(API)——DateUtils:按照上下午显示时间
|
|
|
|
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {
|
|
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
|
|
}
|
|
|
|
|
}//第一个参数arg0是接收到点击事件的对话框
|
|
|
|
|
//第二个参数arg1是该对话框上的按钮
|
|
|
|
|
}
|
|
|
|
|
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,提供了一个日期和时间选择器的界面。
|
|
|
|
|
* 该类支持 24 小时制和 12 小时制(AM/PM)两种模式。
|
|
|
|
|
*/
|
|
|
|
|
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
|
|
|
|
|
|
|
|
|
|
private Calendar mDate = Calendar.getInstance(); // 创建一个 Calendar 实例,用于时间操作
|
|
|
|
|
private boolean mIs24HourView; // 是否为 24 小时制
|
|
|
|
|
private OnDateTimeSetListener mOnDateTimeSetListener; // 日期时间设置监听器
|
|
|
|
|
private DateTimePicker mDateTimePicker; // 日期时间选择器控件
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 日期时间设置监听器接口。
|
|
|
|
|
*/
|
|
|
|
|
public interface OnDateTimeSetListener {
|
|
|
|
|
void OnDateTimeSet(AlertDialog dialog, long date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化 DateTimePickerDialog。
|
|
|
|
|
* @param context 应用上下文。
|
|
|
|
|
* @param date 初始日期时间(毫秒)。
|
|
|
|
|
*/
|
|
|
|
|
public DateTimePickerDialog(Context context, long date) {
|
|
|
|
|
super(context);
|
|
|
|
|
mDateTimePicker = new DateTimePicker(context);
|
|
|
|
|
setView(mDateTimePicker); // 设置对话框的视图
|
|
|
|
|
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); // 将秒数设置为 0
|
|
|
|
|
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 小时制。
|
|
|
|
|
* @param is24HourView true 表示 24 小时制,false 表示 12 小时制。
|
|
|
|
|
*/
|
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
|
|
mIs24HourView = is24HourView;
|
|
|
|
|
mDateTimePicker.set24HourView(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(this.getContext(), date, flag)); // 设置对话框标题
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按钮点击事件处理。
|
|
|
|
|
* @param dialog 接收到点击事件的对话框。
|
|
|
|
|
* @param which 被点击的按钮。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
if (mOnDateTimeSetListener != null) {
|
|
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); // 通知监听器日期时间已设置
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|