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.
Dome/src1/net/micode/notes/ui/DateTimePickerDialog.java

116 lines
4.6 KiB

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