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.
Notes/src/notes/ui/DateTimePickerDialog.java

93 lines
4.2 KiB

/*
* 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);//访问AlertDialog的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); //导入calendar时间
mDate.set(Calendar.SECOND, 0);//秒数设为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()));
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小时格式或12小时格式
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR; //错误应为12HOUR
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); //设置对话框标题
} //日期时间的本地化格式化
public void onClick(DialogInterface arg0, int arg1) {//点击确认按钮时调用
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}
}