/* * 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 { private Calendar mDate = Calendar.getInstance();//便于时间操作 private boolean mIs24HourView; private OnDateTimeSetListener mOnDateTimeSetListener;//时间日期滚动控件 private DateTimePicker mDateTimePicker;//用于设置或获取日期和时间 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对象 mDate.set(Calendar.SECOND, 0);// 将秒数设为0 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 为对话框设置一个确定按钮 setButton(context.getString(R.string.datetime_dialog_ok), this); // 为对话框设置一个取消按钮,传入null为点击事件处理器 setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); //根据当前的时间的时间格式设置是否使用24小时制显示时间 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; // 根据用户选择的,决定是否使用24小时制 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; // 将日期转化为字符串,并设置标题 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); } // 用于处理对话框的点击事件 public void onClick(DialogInterface arg0, int arg1) { // 如果有日期时间设置监听器,则传递当前日期时间 if (mOnDateTimeSetListener != null) { mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); } } }