|
|
/*
|
|
|
* 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;//引入ui包
|
|
|
|
|
|
import java.util.Calendar;//导入日历工具包
|
|
|
|
|
|
import net.micode.notes.R;//引入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类的对象,用于操作日期
|
|
|
private boolean mIs24HourView;//判断当前设置是否为24小时模式
|
|
|
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);//将year设置为mNote的年份
|
|
|
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());//根据从mdate中获取的当前时间设置现有时间
|
|
|
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 =//通过DataUtils按照24时制显示
|
|
|
DateUtils.FORMAT_SHOW_YEAR |//根据date和显示格式设置标题
|
|
|
DateUtils.FORMAT_SHOW_DATE |
|
|
|
DateUtils.FORMAT_SHOW_TIME;
|
|
|
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;//是否为24小时
|
|
|
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));//设置标题
|
|
|
}
|
|
|
|
|
|
public void onClick(DialogInterface arg0, int arg1) {//arg0接收到点击事件的对话框,arg1是该对话框上的按钮
|
|
|
if (mOnDateTimeSetListener != null) {//设置日期时间
|
|
|
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |