|
|
|
@ -28,8 +28,23 @@ import android.view.View;
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @ProjectName:
|
|
|
|
|
* @Package: net.micode.notes.ui
|
|
|
|
|
* @ClassName: DateTimePicker
|
|
|
|
|
* @Description: 用于选择日期和时间的自定义视图,包括年、月、日、时、分等选择器。
|
|
|
|
|
* 它使用了NumberPicker控件来实现日期和时间的选择功能,并提供了一些回调接口以便在日期和时间改变时进行通知。
|
|
|
|
|
* @Author: xumingyang
|
|
|
|
|
* @CreateDate: 2023-12-24 12:58
|
|
|
|
|
* @UpdateUser: 更新者:
|
|
|
|
|
* @UpdateDate: 2023-12-24 12:58
|
|
|
|
|
* @UpdateRemark: 更新说明:
|
|
|
|
|
* @Version: 1.0
|
|
|
|
|
*/
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
//FrameLayout是布局模板之一
|
|
|
|
|
//所有的子元素全部在屏幕的右上方
|
|
|
|
|
private static final boolean DEFAULT_ENABLE_STATE = true;
|
|
|
|
|
|
|
|
|
|
private static final int HOURS_IN_HALF_DAY = 12;
|
|
|
|
@ -45,13 +60,15 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
private static final int MINUT_SPINNER_MAX_VAL = 59;
|
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
//初始化控件
|
|
|
|
|
private final NumberPicker mDateSpinner;
|
|
|
|
|
private final NumberPicker mHourSpinner;
|
|
|
|
|
private final NumberPicker mMinuteSpinner;
|
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
|
//NumberPicker是数字选择器
|
|
|
|
|
//这里定义的四个变量全部是在设置闹钟时需要选择的变量(如日期、时、分、上午或者下午)
|
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
//定义了Calendar类型的变量mDate,用于操作时间
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
|
|
|
|
|
|
private boolean mIsAm;
|
|
|
|
@ -72,8 +89,22 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//OnValueChangeListener,这是时间改变监听器,这里主要是对日期的监听
|
|
|
|
|
//将现在日期的值传递给mDate;updateDateControl是同步操作
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
//这里是对 小时(Hour) 的监听
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method onValueChange
|
|
|
|
|
* @description 这段代码是一个回调函数,用于监听NumberPicker的数值改变事件。
|
|
|
|
|
* 当用户改变了时间选择器中的小时数时,就会触发这个回调函数。
|
|
|
|
|
* 在函数中,首先判断当前选择器是否为24小时制,如果不是则需要处理上下午的切换和跨天的情况。
|
|
|
|
|
* @date: 2023-12-24 13:03
|
|
|
|
|
* @author: xumingyang
|
|
|
|
|
* @param *声明一个Calendar的变量cal,便于后续的操作
|
|
|
|
|
* @return isdateChanged bool类型 判断是否需要对时间进行调整
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
@ -83,29 +114,33 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
//这里是对于12小时制时,晚上11点和12点交替时对日期的更改
|
|
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
}//这里是对于12小时制时,晚上11点和12点交替时对日期的更改
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}//这里是对于12小时制时,中午11点和12点交替时对AM和PM的更改
|
|
|
|
|
} else {
|
|
|
|
|
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
//这里是对于24小时制时,晚上11点和12点交替时对日期的更改
|
|
|
|
|
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
|
isDateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}//这里是对于12小时制时,凌晨11点和12点交替时对日期的更改
|
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
|
//通过数字选择器对newHour的赋值
|
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, newHour);
|
|
|
|
|
//通过set函数将新的Hour值传给mDate
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
if (isDateChanged) {
|
|
|
|
|
setCurrentYear(cal.get(Calendar.YEAR));
|
|
|
|
@ -116,16 +151,25 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
/**
|
|
|
|
|
* @method onValueChange
|
|
|
|
|
* @description 用于监控 NumberPicker(数字选择器)的数值变化事件。
|
|
|
|
|
* 具体来说,这个监听器是用于监听分钟选择器的数值变化。
|
|
|
|
|
* @param *三个参数:picker(当前的 NumberPicker 对象)、oldVal(旧值)和 newVal(新值)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
|
int offset = 0;
|
|
|
|
|
//设置offset,作为小时改变的一个记录数据
|
|
|
|
|
if (oldVal == maxValue && newVal == minValue) {
|
|
|
|
|
offset += 1;
|
|
|
|
|
} else if (oldVal == minValue && newVal == maxValue) {
|
|
|
|
|
offset -= 1;
|
|
|
|
|
}
|
|
|
|
|
//如果原值为59,新值为0,则offset加1
|
|
|
|
|
//如果原值为0,新值为59,则offset减1
|
|
|
|
|
if (offset != 0) {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
|
mHourSpinner.setValue(getCurrentHour());
|
|
|
|
@ -145,9 +189,15 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
/**
|
|
|
|
|
* @method onValueChange
|
|
|
|
|
* @description 实现了一个监听器,用于监测用户对NumberPicker控件数值的改变,
|
|
|
|
|
* 并根据改变的数值来更新界面上的上午/下午控件
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
|
//对AM和PM的监听
|
|
|
|
|
if (mIsAm) {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
|
|
} else {
|
|
|
|
@ -166,18 +216,25 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
//通过对数据库的访问,获取当前的系统时间
|
|
|
|
|
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
|
}
|
|
|
|
|
//上面函数的得到的是一个天文数字(1970至今的秒数),需要DateFormat将其变得有意义
|
|
|
|
|
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
|
super(context);
|
|
|
|
|
//获取系统时间
|
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
|
mInitialising = true;
|
|
|
|
|
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
|
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 如果当前Activity里用到别的layout,比如对话框layout
|
|
|
|
|
* 还要设置这个layout上的其他组件的内容,就必须用inflate()方法先将对话框的layout找出来
|
|
|
|
|
* 然后再用findViewById()找到它上面的其它组件
|
|
|
|
|
*/
|
|
|
|
|
mDateSpinner = (NumberPicker) findViewById(R.id.date);
|
|
|
|
|
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
|
|
|
|
|
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
|
|
|
|
@ -214,17 +271,28 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mInitialising = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method setEnabled
|
|
|
|
|
* @description 这个函数用于统一设置控件及其相关子控件的可用状态,并且会根据传入的参数值进行相应的操作
|
|
|
|
|
* @param *布尔值 enabled,用于指定控件是否可用。
|
|
|
|
|
* @return 将 mIsEnabled 的值更新为传入的 enabled,以保持状态同步
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* 如果传入的参数 enabled 与当前的控件可用状态 mIsEnabled 相同,则无需进行任何操作,直接返回。
|
|
|
|
|
* 这样可以避免重复设置控件的可用状态。
|
|
|
|
|
*/
|
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
|
mDateSpinner.setEnabled(enabled);
|
|
|
|
|
mMinuteSpinner.setEnabled(enabled);
|
|
|
|
|
mHourSpinner.setEnabled(enabled);
|
|
|
|
|
mAmPmSpinner.setEnabled(enabled);
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
|
//将 mIsEnabled 的值更新为传入的 enabled,以保持状态同步
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -239,7 +307,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
*/
|
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
|
}
|
|
|
|
|
}//实现函数——得到当前的秒数
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the current date
|
|
|
|
@ -251,7 +319,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
cal.setTimeInMillis(date);
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}//实现函数功能——设置当前的时间,参数是date
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the current date
|
|
|
|
@ -269,13 +337,14 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
setCurrentDay(dayOfMonth);
|
|
|
|
|
setCurrentHour(hourOfDay);
|
|
|
|
|
setCurrentMinute(minute);
|
|
|
|
|
}
|
|
|
|
|
}//实现函数功能——设置当前的时间,参数是各详细的变量
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current year
|
|
|
|
|
*
|
|
|
|
|
* @return The current year
|
|
|
|
|
*/
|
|
|
|
|
//下面是得到year、month、day等值
|
|
|
|
|
public int getCurrentYear() {
|
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
|
}
|
|
|
|
@ -446,7 +515,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues);
|
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
}
|
|
|
|
|
}// 对于星期几的算法
|
|
|
|
|
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
@ -456,7 +525,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mAmPmSpinner.setValue(index);
|
|
|
|
|
mAmPmSpinner.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}// 对于上下午操作的算法
|
|
|
|
|
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
@ -466,7 +535,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
|
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}// 对与小时的算法
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the callback that indicates the 'Set' button has been pressed.
|
|
|
|
|