/* * 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(); //创建一个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);//秒针归零 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()));//时间24小时格式显示 updateTitle(mDate.getTimeInMillis());//更新标题时间 } public void set24HourView(boolean is24HourView) { mIs24HourView = is24HourView; }//设置24小时格式试图 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;//日期管理工具类,DateUtils:按照上下午显示时间 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;//确认是否24小时格式 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));//设置标题 } public void onClick(DialogInterface arg0, int arg1) { if (mOnDateTimeSetListener != null) { mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); } } //第一个参数arg0是接收到点击事件的对话框,第二个参数arg1是该对话框上的按钮 }