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.
gitProject/other/07_210340164温雍敬_代码标注/ui/DateTimePickerDialog.java

93 lines
4.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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 {
/*该类继承于AlertDialog类实现了OnClickListener接口主要功能是对设置提醒事件这个过程中的对话框界面进行设置该类会弹出一个对话框可以实现若干个按钮对时间进行修改。该类实现了相关的方法有对提醒时间的获取以及时间的表现格式set24HourView方法
并且实现了对对话中标题修改的方法,实现了对点击事件的侦听,主要的功能是对提醒事件相关属性进行修改的一个侦听。*/
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);
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);//设置按钮set
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);//设置按钮cancel
set24HourView(DateFormat.is24HourFormat(this.getContext()));//设置24小时视图
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 |//按照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());//设定日期时间
}
}
}