|
|
|
@ -29,9 +29,11 @@ import android.widget.FrameLayout;
|
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
//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;
|
|
|
|
@ -46,10 +48,14 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
//NumberPicker是数字选择器
|
|
|
|
|
//这里定义的四个变量全部是在设置闹钟时需要选择的变量(如日期、时、分、上午或者下午)
|
|
|
|
|
private final NumberPicker mDateSpinner;
|
|
|
|
|
private final NumberPicker mHourSpinner;
|
|
|
|
|
private final NumberPicker mMinuteSpinner;
|
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
|
|
|
|
|
|
//定义了Calendar类型的变量mDate,用于操作时间
|
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
@ -66,6 +72,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
//OnValueChangeListener,这是时间改变监听器,这里主要是对日期的监听
|
|
|
|
|
//将现在日期的值传递给mDate;updateDateControl是同步操作
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
|
|
|
|
|
updateDateControl();
|
|
|
|
@ -74,37 +82,52 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
//这里是对 小时(Hour) 的监听
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
|
|
|
|
|
|
//声明一个Calendar的变量cal,便于后续的操作
|
|
|
|
|
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;
|
|
|
|
|
//这里是对于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;
|
|
|
|
|
//这里是对于24小时制时,凌晨11点和12点交替时对日期的更改
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//通过数字选择器对newHour的赋值
|
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
|
|
|
|
|
|
//通过set函数将新的Hour值传给mDate
|
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, newHour);
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
if (isDateChanged) {
|
|
|
|
@ -117,10 +140,14 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
//这里是对 分钟(Minute)改变的监听
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int offset = 0;//设置offset,作为小时改变的一个记录数据
|
|
|
|
|
|
|
|
|
|
//如果原值为59,新值为0,则offset加1
|
|
|
|
|
//如果原值为0,新值为59,则offset减
|
|
|
|
|
if (oldVal == maxValue && newVal == minValue) {
|
|
|
|
|
offset += 1;
|
|
|
|
|
} else if (oldVal == minValue && newVal == maxValue) {
|
|
|
|
@ -145,6 +172,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
//对AM和PM的监听
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
@ -163,21 +191,28 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通过对数据库的访问,获取当前的系统时间
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
|
|
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//上面函数的得到的是一个天文数字(1970至今的秒数),需要DateFormat将其变得有意义
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
@ -227,6 +262,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//下面的代码通过原程序的注释已经比较清晰,另外可以通过函数名来判断
|
|
|
|
|
//下面的各函数主要是对上面代码引用到的各函数功能的实现
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return mIsEnabled;
|
|
|
|
@ -237,6 +274,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
*
|
|
|
|
|
* @return the current date in millis
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//实现函数——得到当前的秒数
|
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
|
}
|
|
|
|
@ -246,6 +285,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
*
|
|
|
|
|
* @param date The current date in millis
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//实现函数功能——设置当前的时间,参数是date
|
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTimeInMillis(date);
|
|
|
|
@ -262,6 +303,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
* @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);
|
|
|
|
@ -276,6 +319,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
*
|
|
|
|
|
* @return The current year
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//下面是得到year、month、day等值
|
|
|
|
|
public int getCurrentYear() {
|
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
|
}
|
|
|
|
@ -434,7 +479,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
|
private void updateDateControl() {// 对于星期几的算法
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
|
|
|
|
@ -448,7 +493,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
|
private void updateAmPmControl() {// 对于上下午操作的算法
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
@ -458,7 +503,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
|
private void updateHourControl() {// 对与小时的算法
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
|
|
|
|
|