|
|
|
@ -1,19 +1,4 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 包声明,表明该类属于 net.micode.notes.ui 包
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
|
|
|
|
import java.text.DateFormatSymbols;
|
|
|
|
@ -28,86 +13,128 @@ import android.view.View;
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义 DateTimePicker 类,继承自 FrameLayout,可作为一个日期时间选择器
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
// 定义默认的启用状态,当为 true 时表示该日期时间选择器处于启用状态
|
|
|
|
|
private static final boolean DEFAULT_ENABLE_STATE = true;
|
|
|
|
|
|
|
|
|
|
// 半天包含的小时数,即 12 小时
|
|
|
|
|
private static final int HOURS_IN_HALF_DAY = 12;
|
|
|
|
|
// 一天包含的小时数,即 24 小时
|
|
|
|
|
private static final int HOURS_IN_ALL_DAY = 24;
|
|
|
|
|
// 一周包含的天数,即 7 天
|
|
|
|
|
private static final int DAYS_IN_ALL_WEEK = 7;
|
|
|
|
|
// 日期选择器的最小允许值,用于设置 NumberPicker 的最小范围
|
|
|
|
|
private static final int DATE_SPINNER_MIN_VAL = 0;
|
|
|
|
|
// 日期选择器的最大允许值,用于设置 NumberPicker 的最大范围
|
|
|
|
|
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;
|
|
|
|
|
// 上午/下午选择器的最小允许值,通常 0 表示上午(AM)
|
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
|
// 上午/下午选择器的最大允许值,通常 1 表示下午(PM)
|
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
// 用于选择日期的 NumberPicker 组件
|
|
|
|
|
private final NumberPicker mDateSpinner;
|
|
|
|
|
// 用于选择小时的 NumberPicker 组件
|
|
|
|
|
private final NumberPicker mHourSpinner;
|
|
|
|
|
// 用于选择分钟的 NumberPicker 组件
|
|
|
|
|
private final NumberPicker mMinuteSpinner;
|
|
|
|
|
// 用于选择上午/下午的 NumberPicker 组件
|
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
|
// 存储日期和时间信息的 Calendar 对象,用于对日期和时间进行操作和存储
|
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
// 存储日期显示的字符串数组,用于存储一周内的日期显示信息
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
|
|
|
|
|
|
// 表示当前时间是否为上午,true 表示上午,false 表示下午
|
|
|
|
|
private boolean mIsAm;
|
|
|
|
|
|
|
|
|
|
// 表示是否使用 24 小时制,true 表示使用 24 小时制,false 表示使用 12 小时制
|
|
|
|
|
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 实例,用于临时存储和计算日期的变化
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
|
// 在 12 小时制下
|
|
|
|
|
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
|
|
|
|
|
// 如果从上午的最后一小时(11 点)切换到下午的第一小时(12 点),日期加一天
|
|
|
|
|
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) {
|
|
|
|
|
// 如果从下午的第一小时(12 点)切换到上午的最后一小时(11 点),日期减一天
|
|
|
|
|
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;
|
|
|
|
|
// 切换上午/下午标志
|
|
|
|
|
mIsAm =!mIsAm;
|
|
|
|
|
// 更新上午/下午选择器的显示
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 在 24 小时制下
|
|
|
|
|
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
|
|
|
|
|
// 如果从 23 点切换到 0 点,日期加一天
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
|
|
|
|
|
// 如果从 0 点切换到 23 点,日期减一天
|
|
|
|
|
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);
|
|
|
|
|
// 计算新的小时值,考虑 12 小时制和 24 小时制的转换
|
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
|
// 更新 Calendar 对象的小时值
|
|
|
|
|
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));
|
|
|
|
@ -115,105 +142,159 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 分钟选择器的监听器,当分钟选择器的值发生变化时触发
|
|
|
|
|
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) {
|
|
|
|
|
if (offset!= 0) {
|
|
|
|
|
// 根据分钟的变化更新日期的小时
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
|
// 更新小时选择器的值
|
|
|
|
|
mHourSpinner.setValue(getCurrentHour());
|
|
|
|
|
// 更新日期显示相关的控件
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 获取当前小时
|
|
|
|
|
int newHour = getCurrentHourOfDay();
|
|
|
|
|
if (newHour >= HOURS_IN_HALF_DAY) {
|
|
|
|
|
// 如果超过 12 点,标记为下午
|
|
|
|
|
mIsAm = false;
|
|
|
|
|
// 更新上午/下午选择器的显示
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
} else {
|
|
|
|
|
// 否则标记为上午
|
|
|
|
|
mIsAm = true;
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 更新 Calendar 对象的分钟值
|
|
|
|
|
mDate.set(Calendar.MINUTE, newVal);
|
|
|
|
|
// 调用日期时间更改的回调方法
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 上午/下午选择器的监听器,当上午/下午选择器的值发生变化时触发
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
|
// 切换上午/下午标志
|
|
|
|
|
mIsAm =!mIsAm;
|
|
|
|
|
if (mIsAm) {
|
|
|
|
|
// 如果切换到上午,日期时间减少 12 小时
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果切换到下午,日期时间增加 12 小时
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构造函数,使用当前时间作为初始日期
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构造函数,使用指定的日期作为初始日期
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构造函数,使用指定的日期和是否 24 小时制作为初始设置
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
|
super(context);
|
|
|
|
|
// 获取一个 Calendar 实例
|
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
|
mInitialising = true;
|
|
|
|
|
// 判断当前时间是否为上午
|
|
|
|
|
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
|
|
|
|
|
// 从布局文件中加载该组件
|
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化日期选择器
|
|
|
|
|
mDateSpinner = (NumberPicker) findViewById(R.id.date);
|
|
|
|
|
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
|
|
|
|
|
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
|
|
|
|
|
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化小时选择器
|
|
|
|
|
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
|
|
|
|
|
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化分钟选择器
|
|
|
|
|
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
|
|
|
|
|
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
|
|
|
|
|
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
|
|
|
|
|
// 设置长按更新间隔,以毫秒为单位
|
|
|
|
|
mMinuteSpinner.setOnLongPressUpdateInterval(100);
|
|
|
|
|
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取上午/下午的字符串表示(如 "AM" 和 "PM")
|
|
|
|
|
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
|
|
|
|
|
// 初始化上午/下午选择器
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置是否使用 24 小时制
|
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
|
|
|
|
|
|
// set to current time
|
|
|
|
|
|
|
|
|
|
// 设置当前日期
|
|
|
|
|
setCurrentDate(date);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置启用状态
|
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
|
|
|
|
|
|
// set the content descriptions
|
|
|
|
|
|
|
|
|
|
// 设置内容描述
|
|
|
|
|
mInitialising = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写 setEnabled 方法,用于设置日期时间选择器的启用状态
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
@ -227,145 +308,126 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写 isEnabled 方法,用于检查日期时间选择器是否处于启用状态
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return mIsEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current date in millis
|
|
|
|
|
*
|
|
|
|
|
* @return the current date in millis
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 获取当前日期的毫秒表示
|
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
// 设置当前日期,通过传入年、月、日、小时和分钟
|
|
|
|
|
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() {
|
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current year
|
|
|
|
|
*
|
|
|
|
|
* @param year The current year
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 设置当前年
|
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mDate.set(Calendar.YEAR, year);
|
|
|
|
|
// 更新日期显示相关的控件
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 调用日期时间更改的回调方法
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current month in the year
|
|
|
|
|
*
|
|
|
|
|
* @return The current month in the year
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 获取当前月
|
|
|
|
|
public int getCurrentMonth() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
mDate.set(Calendar.MONTH, month);
|
|
|
|
|
// 更新日期显示相关的控件
|
|
|
|
|
updateDateControl();
|
|
|
|
|
// 调用日期时间更改的回调方法
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current day of the month
|
|
|
|
|
*
|
|
|
|
|
* @return The day of the month
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 获取当前日
|
|
|
|
|
public int getCurrentDay() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前小时,根据是否 24 小时制进行转换
|
|
|
|
|
private int getCurrentHour() {
|
|
|
|
|
if (mIs24HourView){
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
|
} else {
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
|
return hour - HOURS_IN_HALF_DAY;
|
|
|
|
|
} else {
|
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
|
|
|
|
|
return hour == 0? HOURS_IN_HALF_DAY : hour;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
*
|
|
|
|
|
* @param hourOfDay
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 设置 24 小时制下的当前小时
|
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
|
return;
|
|
|
|
@ -383,63 +445,65 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
hourOfDay = HOURS_IN_HALF_DAY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 更新上午/下午选择器的显示
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
mHourSpinner.setValue(hourOfDay);
|
|
|
|
|
// 调用日期时间更改的回调方法
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get currentMinute
|
|
|
|
|
*
|
|
|
|
|
* @return The Current Minute
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 获取当前分钟
|
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
public boolean is24HourView () {
|
|
|
|
|
|
|
|
|
|
// 检查是否使用 24 小时制
|
|
|
|
|
public boolean is24HourView() {
|
|
|
|
|
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) {
|
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mIs24HourView = is24HourView;
|
|
|
|
|
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
|
|
|
|
|
mAmPmSpinner.setVisibility(is24HourView? View.GONE : View.VISIBLE);
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
|
// 更新小时显示相关的控件
|
|
|
|
|
updateHourControl();
|
|
|
|
|
// 设置当前小时
|
|
|
|
|
setCurrentHour(hour);
|
|
|
|
|
// 更新上午/下午显示相关的控件
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新日期显示相关的控件
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
// 将日期向前偏移 4 天(一周的一半减 1)
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
@ -448,16 +512,22 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新上午/下午显示相关的控件
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
// 在 24 小时制下,隐藏上午/下午选择器
|
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
|
int index = mIsAm ? Calendar.AM : Calendar.PM;
|
|
|
|
|
// 根据当前是上午还是下午设置上午/下午选择器的值
|
|
|
|
|
int index = mIsAm? Calendar.AM : Calendar.PM;
|
|
|
|
|
mAmPmSpinner.setValue(index);
|
|
|
|
|
mAmPmSpinner.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新小时显示相关的控件
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
@ -468,16 +538,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 当日期时间发生更改时调用该方法
|
|
|
|
|
private void onDateTimeChanged() {
|
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
|
if (mOnDateTimeChangedListener!= null) {
|
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
|
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
|
|
|
|
|
}
|
|
|
|
|