|
|
|
@ -0,0 +1,655 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.text.DateFormatSymbols;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
// 默认启用状态
|
|
|
|
|
private static final boolean DEFAULT_ENABLE_STATE = true;
|
|
|
|
|
|
|
|
|
|
// 一半天的小时数
|
|
|
|
|
private static final int HOURS_IN_HALF_DAY = 12;
|
|
|
|
|
// 一天的小时数
|
|
|
|
|
private static final int HOURS_IN_ALL_DAY = 24;
|
|
|
|
|
// 一周的天数
|
|
|
|
|
private static final int DAYS_IN_ALL_WEEK = 7;
|
|
|
|
|
// 日期选择器的最小值
|
|
|
|
|
private static final int DATE_SPINNER_MIN_VAL = 0;
|
|
|
|
|
// 日期选择器的最大值
|
|
|
|
|
private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
|
|
|
|
|
// 24小时制小时选择器的最小值
|
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
|
|
|
|
|
// 24小时制小时选择器的最大值
|
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
|
|
|
|
|
// 12小时制小时选择器的最小值
|
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
|
|
|
|
|
// 12小时制小时选择器的最大值
|
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12;
|
|
|
|
|
// 分钟选择器的最小值
|
|
|
|
|
private static final int MINUT_SPINNER_MIN_VAL = 0;
|
|
|
|
|
// 分钟选择器的最大值
|
|
|
|
|
private static final int MINUT_SPINNER_MAX_VAL = 59;
|
|
|
|
|
// AM/PM选择器的最小值
|
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
|
// AM/PM选择器的最大值
|
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
// 日期选择器
|
|
|
|
|
private final NumberPicker mDateSpinner;
|
|
|
|
|
// 小时选择器
|
|
|
|
|
private final NumberPicker mHourSpinner;
|
|
|
|
|
// 分钟选择器
|
|
|
|
|
private final NumberPicker mMinuteSpinner;
|
|
|
|
|
// AM/PM选择器
|
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
|
// 日期对象
|
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
// 日期显示值数组
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
|
|
|
|
|
|
// 是否为AM
|
|
|
|
|
private boolean mIsAm;
|
|
|
|
|
|
|
|
|
|
// 是否为24小时制
|
|
|
|
|
private boolean mIs24HourView;
|
|
|
|
|
|
|
|
|
|
// 是否启用
|
|
|
|
|
private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
|
|
|
|
|
|
|
|
|
|
private boolean mInitialising;
|
|
|
|
|
|
|
|
|
|
private OnDateTimeChangedListener mOnDateTimeChangedListener;
|
|
|
|
|
|
|
|
|
|
// 日期选择器值变化监听器
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
|
|
|
|
|
updateDateControl();
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 小时选择器值变化监听器
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
|
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
|
|
|
|
|
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, newHour);
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
if (isDateChanged) {
|
|
|
|
|
setCurrentYear(cal.get(Calendar.YEAR));
|
|
|
|
|
setCurrentMonth(cal.get(Calendar.MONTH));
|
|
|
|
|
setCurrentDay(cal.get(Calendar.DAY_OF_MONTH));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 分钟选择器值变化监听器
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
|
int offset = 0;
|
|
|
|
|
if (oldVal == maxValue && newVal == minValue) {
|
|
|
|
|
offset += 1;
|
|
|
|
|
} else if (oldVal == minValue && newVal == maxValue) {
|
|
|
|
|
offset -= 1;
|
|
|
|
|
}
|
|
|
|
|
if (offset != 0) {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
|
mHourSpinner.setValue(getCurrentHour());
|
|
|
|
|
updateDateControl();
|
|
|
|
|
int newHour = getCurrentHourOfDay();
|
|
|
|
|
if (newHour >= HOURS_IN_HALF_DAY) {
|
|
|
|
|
mIsAm = false;
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
} else {
|
|
|
|
|
mIsAm = true;
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mDate.set(Calendar.MINUTE, newVal);
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// AM/PM选择器值变化监听器
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
|
if (mIsAm) {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
|
|
} else {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY);
|
|
|
|
|
}
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 日期时间变化监听器接口
|
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
|
void onDateTimeChanged(DateTimePicker view, int year, int month,
|
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数
|
|
|
|
|
// 构造函数,用于创建DateTimePicker对象
|
|
|
|
|
// 参数context:上下文对象,用于获取应用环境和资源
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
|
// 调用另一个构造函数,传入当前系统时间作为默认值
|
|
|
|
|
// System.currentTimeMillis():获取当前系统时间的毫秒值
|
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于创建DateTimePicker对象
|
|
|
|
|
// 参数context:上下文对象,用于获取系统资源和配置
|
|
|
|
|
// 参数date:初始日期时间,以毫秒为单位的长整型数值
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
|
// 调用另一个构造函数,传入当前上下文、初始日期时间和是否使用24小时制
|
|
|
|
|
// DateFormat.is24HourFormat(context)方法用于判断当前系统是否使用24小时制
|
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于初始化DateTimePicker对象
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
|
// 调用父类的构造函数,传入上下文
|
|
|
|
|
super(context);
|
|
|
|
|
// 初始化Calendar对象,用于存储日期和时间
|
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
|
// 设置初始化标志为true,表示正在初始化
|
|
|
|
|
mInitialising = true;
|
|
|
|
|
// 获取当前小时,判断是否为上午或下午
|
|
|
|
|
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
|
|
|
|
|
// 加载布局文件datetime_picker.xml,并将其设置为当前视图
|
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
|
|
|
|
|
|
// 获取日期选择器NumberPicker,并设置其最小值和最大值
|
|
|
|
|
mDateSpinner = (NumberPicker) findViewById(R.id.date);
|
|
|
|
|
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
|
|
|
|
|
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
|
|
|
|
|
// 设置日期选择器的值变化监听器
|
|
|
|
|
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
|
|
|
|
|
|
|
|
|
|
// 获取小时选择器NumberPicker,并设置其值变化监听器
|
|
|
|
|
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
|
|
|
|
|
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
|
|
|
|
|
// 获取分钟选择器NumberPicker,并设置其最小值和最大值
|
|
|
|
|
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
|
|
|
|
|
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
|
|
|
|
|
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
|
|
|
|
|
// 设置分钟选择器的长按更新间隔
|
|
|
|
|
mMinuteSpinner.setOnLongPressUpdateInterval(100);
|
|
|
|
|
// 设置分钟选择器的值变化监听器
|
|
|
|
|
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
|
|
|
|
|
|
|
|
|
|
// 获取上午/下午字符串数组
|
|
|
|
|
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
|
|
|
|
|
// 获取上午/下午选择器NumberPicker,并设置其最小值、最大值和显示的值
|
|
|
|
|
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
|
|
|
|
|
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
|
|
|
|
|
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
|
|
|
|
|
mAmPmSpinner.setDisplayedValues(stringsForAmPm);
|
|
|
|
|
// 设置上午/下午选择器的值变化监听器
|
|
|
|
|
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
|
|
|
|
|
|
|
|
|
|
// update controls to initial state
|
|
|
|
|
updateDateControl();
|
|
|
|
|
updateHourControl();
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
|
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
|
|
|
|
|
|
// set to current time
|
|
|
|
|
setCurrentDate(date);
|
|
|
|
|
|
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
|
|
|
|
|
|
// set the content descriptions
|
|
|
|
|
mInitialising = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
|
// 检查当前状态是否与要设置的状态相同,如果相同则直接返回,避免重复设置
|
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 调用父类的setEnabled方法,设置组件的基本启用状态
|
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
|
// 设置日期选择器的启用状态
|
|
|
|
|
mDateSpinner.setEnabled(enabled);
|
|
|
|
|
// 设置分钟选择器的启用状态
|
|
|
|
|
mMinuteSpinner.setEnabled(enabled);
|
|
|
|
|
// 设置小时选择器的启用状态
|
|
|
|
|
mHourSpinner.setEnabled(enabled);
|
|
|
|
|
// 设置上午/下午选择器的启用状态
|
|
|
|
|
mAmPmSpinner.setEnabled(enabled);
|
|
|
|
|
// 更新当前组件的启用状态标志
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写父类或接口中的isEnabled方法
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
// 返回成员变量mIsEnabled的值,表示当前对象是否启用
|
|
|
|
|
return mIsEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current date in millis
|
|
|
|
|
*
|
|
|
|
|
* @return the current date in millis
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,返回当前日期的时间戳(以毫秒为单位)
|
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
// 调用mDate对象的getTimeInMillis方法,获取当前日期的时间戳
|
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the current date
|
|
|
|
|
*
|
|
|
|
|
* @param date The current date in millis
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个方法用于设置当前日期和时间
|
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
|
// 获取一个Calendar实例
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
// 将传入的日期(以毫秒为单位)设置到Calendar对象中
|
|
|
|
|
cal.setTimeInMillis(date);
|
|
|
|
|
// 调用另一个重载的setCurrentDate方法,传入年、月、日、小时和分钟
|
|
|
|
|
setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
|
|
|
|
|
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the current date
|
|
|
|
|
*
|
|
|
|
|
* @param year The current year
|
|
|
|
|
* @param month The current month
|
|
|
|
|
* @param dayOfMonth The current dayOfMonth
|
|
|
|
|
* @param hourOfDay The current hourOfDay
|
|
|
|
|
* @param minute The current minute
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个方法用于设置当前日期和时间
|
|
|
|
|
public void setCurrentDate(int year, int month,
|
|
|
|
|
int dayOfMonth, int hourOfDay, int minute) {
|
|
|
|
|
// 调用设置年份的方法,传入年份参数
|
|
|
|
|
setCurrentYear(year);
|
|
|
|
|
// 调用设置月份的方法,传入月份参数
|
|
|
|
|
setCurrentMonth(month);
|
|
|
|
|
// 调用设置日期的方法,传入日期参数
|
|
|
|
|
setCurrentDay(dayOfMonth);
|
|
|
|
|
// 调用设置小时的方法,传入小时参数
|
|
|
|
|
setCurrentHour(hourOfDay);
|
|
|
|
|
// 调用设置分钟的方法,传入分钟参数
|
|
|
|
|
setCurrentMinute(minute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current year
|
|
|
|
|
*
|
|
|
|
|
* @return The current year
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,用于获取当前年份
|
|
|
|
|
public int getCurrentYear() {
|
|
|
|
|
// 调用mDate对象的get方法,传入Calendar.YEAR常量,获取当前年份
|
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current year
|
|
|
|
|
*
|
|
|
|
|
* @param year The current year
|
|
|
|
|
*/
|
|
|
|
|
// 设置当前年份的方法
|
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
|
// 检查是否正在初始化以及年份是否与当前年份相同
|
|
|
|
|
// 如果正在初始化或者年份未改变,则直接返回,不进行后续操作
|
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 使用Calendar对象设置日期的年份
|
|
|
|
|
mDate.set(Calendar.YEAR, year);
|
|
|
|
|
// 更新日期控件,以反映新的年份
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 调用方法处理日期时间变化,可能触发相关事件或更新UI
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current month in the year
|
|
|
|
|
*
|
|
|
|
|
* @return The current month in the year
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,用于获取当前月份
|
|
|
|
|
public int getCurrentMonth() {
|
|
|
|
|
// 调用mDate对象的get方法,传入Calendar.MONTH常量,获取当前日期中的月份
|
|
|
|
|
// Calendar.MONTH的值是从0开始的,即0代表一月,1代表二月,依此类推
|
|
|
|
|
return mDate.get(Calendar.MONTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current month in the year
|
|
|
|
|
*
|
|
|
|
|
* @param month The month in the year
|
|
|
|
|
*/
|
|
|
|
|
// 设置当前月份的方法
|
|
|
|
|
public void setCurrentMonth(int month) {
|
|
|
|
|
// 检查是否正在初始化以及传入的月份是否与当前月份相同
|
|
|
|
|
// 如果正在初始化或者月份相同,则直接返回,不进行任何操作
|
|
|
|
|
if (!mInitialising && month == getCurrentMonth()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 使用Calendar对象设置日期的月份
|
|
|
|
|
mDate.set(Calendar.MONTH, month);
|
|
|
|
|
// 更新日期控件,以反映新的月份
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 调用onDateTimeChanged方法,通知日期时间已更改
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current day of the month
|
|
|
|
|
*
|
|
|
|
|
* @return The day of the month
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,用于获取当前日期的天数
|
|
|
|
|
public int getCurrentDay() {
|
|
|
|
|
// 调用Calendar实例mDate的get方法,传入Calendar.DAY_OF_MONTH常量
|
|
|
|
|
// 该常量表示获取当前日期中的天数
|
|
|
|
|
// 返回当前日期的天数
|
|
|
|
|
return mDate.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current day of the month
|
|
|
|
|
*
|
|
|
|
|
* @param dayOfMonth The day of the month
|
|
|
|
|
*/
|
|
|
|
|
// 设置当前日期的天数
|
|
|
|
|
public void setCurrentDay(int dayOfMonth) {
|
|
|
|
|
// 检查是否正在初始化以及传入的天数是否与当前天数相同
|
|
|
|
|
if (!mInitialising && dayOfMonth == getCurrentDay()) {
|
|
|
|
|
// 如果相同,则直接返回,不做任何操作
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 使用Calendar对象设置日期的天数
|
|
|
|
|
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
|
|
|
|
// 更新日期控件,以反映新的日期
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 触发日期时间变化事件,通知相关监听器
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
* @return The current hour in 24 hour mode
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,用于获取当前的小时数(24小时制)
|
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
|
// 调用mDate对象的get方法,传入Calendar.HOUR_OF_DAY常量
|
|
|
|
|
// 该常量表示获取当前时间的小时数(24小时制)
|
|
|
|
|
// 返回获取到的小时数
|
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前小时数的私有方法
|
|
|
|
|
private int getCurrentHour() {
|
|
|
|
|
// 检查是否为24小时制视图
|
|
|
|
|
if (mIs24HourView){
|
|
|
|
|
// 如果是24小时制,直接返回当前的小时数
|
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
|
} else {
|
|
|
|
|
// 如果不是24小时制,先获取当前的小时数
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
|
// 检查当前小时数是否超过半天的小时数(12小时)
|
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
|
// 如果超过12小时,返回减去半天小时数的结果(转换为下午的小时数)
|
|
|
|
|
return hour - HOURS_IN_HALF_DAY;
|
|
|
|
|
} else {
|
|
|
|
|
// 如果当前小时数为0(即午夜),则返回半天小时数(12)
|
|
|
|
|
// 否则,直接返回当前小时数(上午的小时数)
|
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
*
|
|
|
|
|
* @param hourOfDay
|
|
|
|
|
*/
|
|
|
|
|
// 设置当前小时的方法
|
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
|
// 检查是否正在初始化且小时数与当前小时相同,如果是则直接返回
|
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 使用Calendar对象设置小时数
|
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
|
|
|
|
// 检查是否为24小时制
|
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
|
// 如果小时数大于等于半天的小时数(12小时),则设置为下午
|
|
|
|
|
if (hourOfDay >= HOURS_IN_HALF_DAY) {
|
|
|
|
|
mIsAm = false;
|
|
|
|
|
// 如果小时数大于半天的小时数,则减去半天的小时数
|
|
|
|
|
if (hourOfDay > HOURS_IN_HALF_DAY) {
|
|
|
|
|
hourOfDay -= HOURS_IN_HALF_DAY;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 否则设置为上午
|
|
|
|
|
mIsAm = true;
|
|
|
|
|
// 如果小时数为0,则设置为半天的小时数
|
|
|
|
|
if (hourOfDay == 0) {
|
|
|
|
|
hourOfDay = HOURS_IN_HALF_DAY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 更新上午/下午控件
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
// 设置小时选择器的值
|
|
|
|
|
mHourSpinner.setValue(hourOfDay);
|
|
|
|
|
// 触发时间变化事件
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get currentMinute
|
|
|
|
|
*
|
|
|
|
|
* @return The Current Minute
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共方法,用于获取当前分钟数
|
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
|
// 调用mDate对象的get方法,传入Calendar.MINUTE常量,获取当前时间的分钟数
|
|
|
|
|
return mDate.get(Calendar.MINUTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current minute
|
|
|
|
|
*/
|
|
|
|
|
// 设置当前分钟的方法
|
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
|
// 检查是否正在初始化并且分钟值是否与当前分钟相同
|
|
|
|
|
// 如果是,则直接返回,不进行任何操作
|
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 设置分钟选择器的值为传入的分钟值
|
|
|
|
|
mMinuteSpinner.setValue(minute);
|
|
|
|
|
// 更新内部日期对象的分钟字段为传入的分钟值
|
|
|
|
|
mDate.set(Calendar.MINUTE, minute);
|
|
|
|
|
// 调用方法通知日期时间已更改
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return true if this is in 24 hour view else false.
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个公共的方法,用于判断是否为24小时制视图
|
|
|
|
|
public boolean is24HourView () {
|
|
|
|
|
// 返回成员变量mIs24HourView的值,该变量用于存储是否为24小时制的状态
|
|
|
|
|
return mIs24HourView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set whether in 24 hour or AM/PM mode.
|
|
|
|
|
*
|
|
|
|
|
* @param is24HourView True for 24 hour mode. False for AM/PM mode.
|
|
|
|
|
*/
|
|
|
|
|
// 设置24小时制视图
|
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
|
|
// 如果当前24小时制视图状态与传入的参数相同,则直接返回,不做任何操作
|
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 更新24小时制视图状态
|
|
|
|
|
mIs24HourView = is24HourView;
|
|
|
|
|
// 根据是否为24小时制视图,设置上午/下午选择器的可见性
|
|
|
|
|
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
|
|
|
|
|
// 获取当前的小时数
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
|
// 更新小时控制部分
|
|
|
|
|
updateHourControl();
|
|
|
|
|
// 设置当前小时数
|
|
|
|
|
setCurrentHour(hour);
|
|
|
|
|
// 更新上午/下午控制部分
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新日期控件的方法
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
|
// 获取当前时间的Calendar实例
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
// 将Calendar实例的时间设置为mDate的时间
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
// 将日期向前调整一周的一半再加一天,即调整到一周的起始位置
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
|
|
|
|
|
// 清除日期选择器的显示值
|
|
|
|
|
mDateSpinner.setDisplayedValues(null);
|
|
|
|
|
// 遍历一周的每一天
|
|
|
|
|
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
|
|
|
|
|
// 每次循环将日期加一天
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
// 将格式化后的日期字符串存储到数组中
|
|
|
|
|
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
|
|
|
|
|
}
|
|
|
|
|
// 设置日期选择器的显示值为更新后的日期数组
|
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues);
|
|
|
|
|
// 设置日期选择器的当前值为一周的中间位置
|
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
|
|
|
|
|
// 刷新日期选择器,使其显示最新的值
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个私有方法,用于更新上午/下午控制组件的显示状态
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
|
// 检查是否为24小时制视图
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
// 如果是24小时制视图,则隐藏上午/下午选择器
|
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果不是24小时制视图,则根据当前是上午还是下午设置选择器的值
|
|
|
|
|
int index = mIsAm ? Calendar.AM : Calendar.PM;
|
|
|
|
|
mAmPmSpinner.setValue(index);
|
|
|
|
|
// 显示上午/下午选择器
|
|
|
|
|
mAmPmSpinner.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个私有方法updateHourControl,用于更新小时选择器的范围
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
|
// 检查是否为24小时制视图
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
// 如果是24小时制视图,设置小时选择器的最小值为HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
|
// 设置小时选择器的最大值为HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW
|
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果不是24小时制视图,设置小时选择器的最小值为HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
|
|
|
|
|
// 设置小时选择器的最大值为HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW
|
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the callback that indicates the 'Set' button has been pressed.
|
|
|
|
|
* @param callback the callback, if null will do nothing
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个方法,用于设置日期时间变化监听器
|
|
|
|
|
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
|
|
|
|
|
// 将传入的监听器对象赋值给成员变量mOnDateTimeChangedListener
|
|
|
|
|
mOnDateTimeChangedListener = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 定义一个私有方法,用于处理日期时间变化事件
|
|
|
|
|
private void onDateTimeChanged() {
|
|
|
|
|
// 检查是否设置了日期时间变化监听器
|
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
|
// 如果监听器不为空,则调用监听器的onDateTimeChanged方法
|
|
|
|
|
// 传递当前对象以及当前的年、月、日、小时和分钟
|
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
|
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|