|
|
@ -21,124 +21,92 @@ import java.util.Calendar;
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 日期时间选择器控件,用于选择日期和时间
|
|
|
|
|
|
|
|
* 支持12小时制和24小时制显示模式
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
// 默认启用状态
|
|
|
|
|
|
|
|
private static final boolean DEFAULT_ENABLE_STATE = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 时间相关常量
|
|
|
|
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 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_MIN_VAL = 0;
|
|
|
|
private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
|
|
|
|
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;
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
|
|
|
|
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;
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 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_MIN_VAL = 0;
|
|
|
|
private static final int MINUT_SPINNER_MAX_VAL = 59;
|
|
|
|
private static final int MINUT_SPINNER_MAX_VAL = 59;
|
|
|
|
|
|
|
|
|
|
|
|
// AM/PM选择器范围
|
|
|
|
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// 控件成员变量
|
|
|
|
private final NumberPicker mDateSpinner;
|
|
|
|
private final NumberPicker mDateSpinner; // 日期选择器
|
|
|
|
private final NumberPicker mHourSpinner;
|
|
|
|
private final NumberPicker mHourSpinner; // 小时选择器
|
|
|
|
private final NumberPicker mMinuteSpinner;
|
|
|
|
private final NumberPicker mMinuteSpinner; // 分钟选择器
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
private final NumberPicker mAmPmSpinner; // AM/PM选择器
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
|
|
private Calendar mDate; // 当前选择的日期时间
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
|
|
|
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; // 日期显示值数组
|
|
|
|
private boolean mIsAm;
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mIsAm; // 是否为上午
|
|
|
|
private boolean mIs24HourView;
|
|
|
|
private boolean mIs24HourView; // 是否为24小时制
|
|
|
|
|
|
|
|
private boolean mIsEnabled = DEFAULT_ENABLE_STATE; // 控件是否可用
|
|
|
|
|
|
|
|
private boolean mInitialising; // 是否正在初始化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OnDateTimeChangedListener mOnDateTimeChangedListener; // 日期时间变化监听器
|
|
|
|
private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mInitialising;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OnDateTimeChangedListener mOnDateTimeChangedListener;
|
|
|
|
|
|
|
|
|
|
|
|
// 日期变化监听器
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
// 计算日期变化量并更新日期
|
|
|
|
|
|
|
|
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
|
|
|
|
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
|
|
|
|
updateDateControl();
|
|
|
|
updateDateControl();
|
|
|
|
onDateTimeChanged();
|
|
|
|
onDateTimeChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 小时变化监听器
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
|
|
|
|
|
|
|
// 处理12小时制下的特殊时间点变化
|
|
|
|
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
// 处理PM到AM的日期变化
|
|
|
|
|
|
|
|
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
|
|
|
|
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
isDateChanged = true;
|
|
|
|
isDateChanged = true;
|
|
|
|
}
|
|
|
|
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
// 处理AM到PM的日期变化
|
|
|
|
|
|
|
|
else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
isDateChanged = true;
|
|
|
|
isDateChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理AM/PM切换
|
|
|
|
|
|
|
|
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
|
|
|
|
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
|
|
|
|
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
updateAmPmControl();
|
|
|
|
updateAmPmControl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 处理24小时制下的特殊时间点变化
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
// 处理23点到0点的日期变化
|
|
|
|
|
|
|
|
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
|
|
|
|
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
isDateChanged = true;
|
|
|
|
isDateChanged = true;
|
|
|
|
}
|
|
|
|
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
|
|
|
|
// 处理0点到23点的日期变化
|
|
|
|
|
|
|
|
else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
|
|
|
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -1);
|
|
|
|
isDateChanged = true;
|
|
|
|
isDateChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新当前小时并触发变化事件
|
|
|
|
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, newHour);
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, newHour);
|
|
|
|
onDateTimeChanged();
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要更新日期
|
|
|
|
|
|
|
|
if (isDateChanged) {
|
|
|
|
if (isDateChanged) {
|
|
|
|
setCurrentYear(cal.get(Calendar.YEAR));
|
|
|
|
setCurrentYear(cal.get(Calendar.YEAR));
|
|
|
|
setCurrentMonth(cal.get(Calendar.MONTH));
|
|
|
|
setCurrentMonth(cal.get(Calendar.MONTH));
|
|
|
@ -147,28 +115,21 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 分钟变化监听器
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
int offset = 0;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 处理分钟循环滚动
|
|
|
|
|
|
|
|
if (oldVal == maxValue && newVal == minValue) {
|
|
|
|
if (oldVal == maxValue && newVal == minValue) {
|
|
|
|
offset += 1; // 从最大值滚动到最小值,小时加1
|
|
|
|
offset += 1;
|
|
|
|
} else if (oldVal == minValue && newVal == maxValue) {
|
|
|
|
} else if (oldVal == minValue && newVal == maxValue) {
|
|
|
|
offset -= 1; // 从最小值滚动到最大值,小时减1
|
|
|
|
offset -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要调整小时
|
|
|
|
|
|
|
|
if (offset != 0) {
|
|
|
|
if (offset != 0) {
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
mHourSpinner.setValue(getCurrentHour());
|
|
|
|
mHourSpinner.setValue(getCurrentHour());
|
|
|
|
updateDateControl();
|
|
|
|
updateDateControl();
|
|
|
|
|
|
|
|
|
|
|
|
// 更新AM/PM显示
|
|
|
|
|
|
|
|
int newHour = getCurrentHourOfDay();
|
|
|
|
int newHour = getCurrentHourOfDay();
|
|
|
|
if (newHour >= HOURS_IN_HALF_DAY) {
|
|
|
|
if (newHour >= HOURS_IN_HALF_DAY) {
|
|
|
|
mIsAm = false;
|
|
|
|
mIsAm = false;
|
|
|
@ -178,18 +139,14 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
updateAmPmControl();
|
|
|
|
updateAmPmControl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置新分钟并触发变化事件
|
|
|
|
|
|
|
|
mDate.set(Calendar.MINUTE, newVal);
|
|
|
|
mDate.set(Calendar.MINUTE, newVal);
|
|
|
|
onDateTimeChanged();
|
|
|
|
onDateTimeChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// AM/PM变化监听器
|
|
|
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
// 切换AM/PM状态并调整时间
|
|
|
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
if (mIsAm) {
|
|
|
|
if (mIsAm) {
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
@ -201,58 +158,39 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 日期时间变化监听接口
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
void onDateTimeChanged(DateTimePicker view, int year, int month,
|
|
|
|
void onDateTimeChanged(DateTimePicker view, int year, int month,
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数,使用当前时间初始化
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数,使用指定时间戳初始化
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 主构造函数,完成所有初始化工作
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
super(context);
|
|
|
|
super(context);
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
mInitialising = true;
|
|
|
|
mInitialising = true;
|
|
|
|
mIsAm = getCurrentHourOfDay() < HOURS_IN_HALF_DAY; // 根据当前时间设置AM/PM
|
|
|
|
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
|
|
|
|
|
|
|
|
|
|
|
|
// 加载布局
|
|
|
|
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化日期选择器
|
|
|
|
|
|
|
|
mDateSpinner = (NumberPicker) findViewById(R.id.date);
|
|
|
|
mDateSpinner = (NumberPicker) findViewById(R.id.date);
|
|
|
|
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
|
|
|
|
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
|
|
|
|
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
|
|
|
|
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
|
|
|
|
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
|
|
|
|
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化小时选择器
|
|
|
|
|
|
|
|
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
|
|
|
|
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
|
|
|
|
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
|
|
|
|
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化分钟选择器
|
|
|
|
|
|
|
|
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
|
|
|
|
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
|
|
|
|
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
|
|
|
|
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
|
|
|
|
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
|
|
|
|
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
|
|
|
|
mMinuteSpinner.setOnLongPressUpdateInterval(100); // 设置长按更新间隔
|
|
|
|
mMinuteSpinner.setOnLongPressUpdateInterval(100);
|
|
|
|
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
|
|
|
|
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化AM/PM选择器
|
|
|
|
|
|
|
|
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
|
|
|
|
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
|
|
|
|
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
|
|
|
|
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
|
|
|
|
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
|
|
|
|
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
|
|
|
@ -260,21 +198,19 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
mAmPmSpinner.setDisplayedValues(stringsForAmPm);
|
|
|
|
mAmPmSpinner.setDisplayedValues(stringsForAmPm);
|
|
|
|
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
|
|
|
|
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新控件初始状态
|
|
|
|
// update controls to initial state
|
|
|
|
updateDateControl();
|
|
|
|
updateDateControl();
|
|
|
|
updateHourControl();
|
|
|
|
updateHourControl();
|
|
|
|
updateAmPmControl();
|
|
|
|
updateAmPmControl();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置时间显示模式
|
|
|
|
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置为当前时间
|
|
|
|
// set to current time
|
|
|
|
setCurrentDate(date);
|
|
|
|
setCurrentDate(date);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置启用状态
|
|
|
|
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化完成
|
|
|
|
// set the content descriptions
|
|
|
|
mInitialising = false;
|
|
|
|
mInitialising = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -284,7 +220,6 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
// 设置各子控件的启用状态
|
|
|
|
|
|
|
|
mDateSpinner.setEnabled(enabled);
|
|
|
|
mDateSpinner.setEnabled(enabled);
|
|
|
|
mMinuteSpinner.setEnabled(enabled);
|
|
|
|
mMinuteSpinner.setEnabled(enabled);
|
|
|
|
mHourSpinner.setEnabled(enabled);
|
|
|
|
mHourSpinner.setEnabled(enabled);
|
|
|
@ -298,14 +233,18 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前日期的时间戳
|
|
|
|
* Get the current date in millis
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return the current date in millis
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前日期(通过时间戳)
|
|
|
|
* Set the current date
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param date The current date in millis
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
@ -315,7 +254,13 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前日期(通过年月日时分)
|
|
|
|
* 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,
|
|
|
|
public void setCurrentDate(int year, int month,
|
|
|
|
int dayOfMonth, int hourOfDay, int minute) {
|
|
|
|
int dayOfMonth, int hourOfDay, int minute) {
|
|
|
@ -327,14 +272,18 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前年份
|
|
|
|
* Get current year
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return The current year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentYear() {
|
|
|
|
public int getCurrentYear() {
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前年份
|
|
|
|
* Set current year
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param year The current year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
@ -346,14 +295,18 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前月份
|
|
|
|
* Get current month in the year
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return The current month in the year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentMonth() {
|
|
|
|
public int getCurrentMonth() {
|
|
|
|
return mDate.get(Calendar.MONTH);
|
|
|
|
return mDate.get(Calendar.MONTH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前月份
|
|
|
|
* Set current month in the year
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param month The month in the year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentMonth(int month) {
|
|
|
|
public void setCurrentMonth(int month) {
|
|
|
|
if (!mInitialising && month == getCurrentMonth()) {
|
|
|
|
if (!mInitialising && month == getCurrentMonth()) {
|
|
|
@ -365,14 +318,18 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前日期
|
|
|
|
* Get current day of the month
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return The day of the month
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentDay() {
|
|
|
|
public int getCurrentDay() {
|
|
|
|
return 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) {
|
|
|
|
public void setCurrentDay(int dayOfMonth) {
|
|
|
|
if (!mInitialising && dayOfMonth == getCurrentDay()) {
|
|
|
|
if (!mInitialising && dayOfMonth == getCurrentDay()) {
|
|
|
@ -384,38 +341,36 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前小时(24小时制)
|
|
|
|
* Get current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
|
|
|
* @return The current hour in 24 hour mode
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取当前小时(根据显示模式)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int getCurrentHour() {
|
|
|
|
private int getCurrentHour() {
|
|
|
|
if (mIs24HourView){
|
|
|
|
if (mIs24HourView){
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
return hour - HOURS_IN_HALF_DAY; // PM时间转换为12小时制
|
|
|
|
return hour - HOURS_IN_HALF_DAY;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour; // 0点转换为12点
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前小时(24小时制)
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param hourOfDay
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理12小时制下的显示
|
|
|
|
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
if (hourOfDay >= HOURS_IN_HALF_DAY) {
|
|
|
|
if (hourOfDay >= HOURS_IN_HALF_DAY) {
|
|
|
|
mIsAm = false;
|
|
|
|
mIsAm = false;
|
|
|
@ -435,14 +390,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前分钟
|
|
|
|
* Get currentMinute
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return The Current Minute
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
return mDate.get(Calendar.MINUTE);
|
|
|
|
return mDate.get(Calendar.MINUTE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置当前分钟
|
|
|
|
* Set current minute
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
@ -454,14 +411,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 是否为24小时制显示
|
|
|
|
* @return true if this is in 24 hour view else false.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public boolean is24HourView() {
|
|
|
|
public boolean is24HourView () {
|
|
|
|
return mIs24HourView;
|
|
|
|
return mIs24HourView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置时间显示模式(24小时制或12小时制)
|
|
|
|
* Set whether in 24 hour or AM/PM mode.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param is24HourView True for 24 hour mode. False for AM/PM mode.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
@ -475,29 +434,20 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
updateAmPmControl();
|
|
|
|
updateAmPmControl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新日期控件显示
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
private void updateDateControl() {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
|
|
|
|
mDateSpinner.setDisplayedValues(null);
|
|
|
|
mDateSpinner.setDisplayedValues(null);
|
|
|
|
|
|
|
|
|
|
|
|
// 生成一周的日期显示文本
|
|
|
|
|
|
|
|
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
|
|
|
|
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
|
|
|
|
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues);
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues);
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); // 设置中间位置为当前日期
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新AM/PM控件显示
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
@ -508,9 +458,6 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新小时控件显示范围
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
private void updateHourControl() {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
@ -522,15 +469,13 @@ 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) {
|
|
|
|
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
|
|
|
|
mOnDateTimeChangedListener = callback;
|
|
|
|
mOnDateTimeChangedListener = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 触发日期时间变化事件
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void onDateTimeChanged() {
|
|
|
|
private void onDateTimeChanged() {
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|