|
|
|
@ -21,20 +21,28 @@ 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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DateTimePicker 类用于创建日期和时间选择器。
|
|
|
|
|
*/
|
|
|
|
|
public class DateTimePicker extends FrameLayout {
|
|
|
|
|
|
|
|
|
|
// 默认使能状态
|
|
|
|
|
private static final boolean DEFAULT_ENABLE_STATE = true;
|
|
|
|
|
|
|
|
|
|
// 12小时制和24小时制的小时数
|
|
|
|
|
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;
|
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
|
|
|
|
@ -46,36 +54,51 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
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;
|
|
|
|
|
// AM/PM选择器
|
|
|
|
|
private final NumberPicker mAmPmSpinner;
|
|
|
|
|
// 当前日期
|
|
|
|
|
private Calendar mDate;
|
|
|
|
|
|
|
|
|
|
// 日期显示值
|
|
|
|
|
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
|
|
|
|
|
|
|
|
|
|
// 是否为上午
|
|
|
|
|
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) {
|
|
|
|
@ -115,9 +138,11 @@ 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;
|
|
|
|
@ -144,9 +169,11 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// AM/PM改变监听器
|
|
|
|
|
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
|
|
// 更新AM/PM
|
|
|
|
|
mIsAm = !mIsAm;
|
|
|
|
|
if (mIsAm) {
|
|
|
|
|
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
|
|
|
|
@ -158,19 +185,37 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 日期时间改变监听器接口。
|
|
|
|
|
*/
|
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
|
void onDateTimeChanged(DateTimePicker view, int year, int month,
|
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数。
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
*/
|
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
|
this(context, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数。
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param date 当前日期
|
|
|
|
|
*/
|
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数。
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param date 当前日期
|
|
|
|
|
* @param is24HourView 是否为24小时制
|
|
|
|
|
*/
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
|
super(context);
|
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
@ -191,6 +236,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
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);
|
|
|
|
@ -198,22 +244,28 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置使能状态。
|
|
|
|
|
* @param enabled 是否使能
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
@ -227,24 +279,26 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取使能状态。
|
|
|
|
|
* @return 是否使能
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return mIsEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current date in millis
|
|
|
|
|
*
|
|
|
|
|
* @return the current date in millis
|
|
|
|
|
* 获取当前日期的时间戳。
|
|
|
|
|
* @return 当前日期的时间戳
|
|
|
|
|
*/
|
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the current date
|
|
|
|
|
*
|
|
|
|
|
* @param date The current date in millis
|
|
|
|
|
* 设置当前日期。
|
|
|
|
|
* @param date 当前日期的时间戳
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
@ -254,13 +308,12 @@ 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
|
|
|
|
|
* 设置当前日期。
|
|
|
|
|
* @param year 当前年份
|
|
|
|
|
* @param month 当前月份
|
|
|
|
|
* @param dayOfMonth 当前日期
|
|
|
|
|
* @param hourOfDay 当前小时
|
|
|
|
|
* @param minute 当前分钟
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentDate(int year, int month,
|
|
|
|
|
int dayOfMonth, int hourOfDay, int minute) {
|
|
|
|
@ -272,18 +325,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current year
|
|
|
|
|
*
|
|
|
|
|
* @return The current year
|
|
|
|
|
* 获取当前年份。
|
|
|
|
|
* @return 当前年份
|
|
|
|
|
*/
|
|
|
|
|
public int getCurrentYear() {
|
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current year
|
|
|
|
|
*
|
|
|
|
|
* @param year The current year
|
|
|
|
|
* 设置当前年份。
|
|
|
|
|
* @param year 当前年份
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
@ -295,18 +346,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current month in the year
|
|
|
|
|
*
|
|
|
|
|
* @return The current month in the year
|
|
|
|
|
* 获取当前月份。
|
|
|
|
|
* @return 当前月份
|
|
|
|
|
*/
|
|
|
|
|
public int getCurrentMonth() {
|
|
|
|
|
return mDate.get(Calendar.MONTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current month in the year
|
|
|
|
|
*
|
|
|
|
|
* @param month The month in the year
|
|
|
|
|
* 设置当前月份。
|
|
|
|
|
* @param month 当前月份
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentMonth(int month) {
|
|
|
|
|
if (!mInitialising && month == getCurrentMonth()) {
|
|
|
|
@ -318,18 +367,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current day of the month
|
|
|
|
|
*
|
|
|
|
|
* @return The day of the month
|
|
|
|
|
* 获取当前日期。
|
|
|
|
|
* @return 当前日期
|
|
|
|
|
*/
|
|
|
|
|
public int getCurrentDay() {
|
|
|
|
|
return mDate.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current day of the month
|
|
|
|
|
*
|
|
|
|
|
* @param dayOfMonth The day of the month
|
|
|
|
|
* 设置当前日期。
|
|
|
|
|
* @param dayOfMonth 当前日期
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentDay(int dayOfMonth) {
|
|
|
|
|
if (!mInitialising && dayOfMonth == getCurrentDay()) {
|
|
|
|
@ -341,15 +388,19 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
* @return The current hour in 24 hour mode
|
|
|
|
|
* 获取当前小时(24小时制)。
|
|
|
|
|
* @return 当前小时(24小时制)
|
|
|
|
|
*/
|
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前小时(12小时制或24小时制)。
|
|
|
|
|
* @return 当前小时
|
|
|
|
|
*/
|
|
|
|
|
private int getCurrentHour() {
|
|
|
|
|
if (mIs24HourView){
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
|
} else {
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
@ -362,9 +413,8 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
|
*
|
|
|
|
|
* @param hourOfDay
|
|
|
|
|
* 设置当前小时(24小时制)。
|
|
|
|
|
* @param hourOfDay 当前小时(24小时制)
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
@ -390,16 +440,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get currentMinute
|
|
|
|
|
*
|
|
|
|
|
* @return The Current Minute
|
|
|
|
|
* 获取当前分钟。
|
|
|
|
|
* @return 当前分钟
|
|
|
|
|
*/
|
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
|
return mDate.get(Calendar.MINUTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set current minute
|
|
|
|
|
* 设置当前分钟。
|
|
|
|
|
* @param minute 当前分钟
|
|
|
|
|
*/
|
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
|
@ -411,16 +461,16 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return true if this is in 24 hour view else false.
|
|
|
|
|
* 获取是否为24小时制。
|
|
|
|
|
* @return 是否为24小时制
|
|
|
|
|
*/
|
|
|
|
|
public boolean is24HourView () {
|
|
|
|
|
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小时制。
|
|
|
|
|
* @param is24HourView 是否为24小时制
|
|
|
|
|
*/
|
|
|
|
|
public void set24HourView(boolean is24HourView) {
|
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
|
@ -434,6 +484,9 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新日期控件。
|
|
|
|
|
*/
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
@ -448,6 +501,9 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新AM/PM控件。
|
|
|
|
|
*/
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
@ -458,6 +514,9 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新小时控件。
|
|
|
|
|
*/
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
@ -469,13 +528,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
|
|
|
|
|
* 设置日期时间改变监听器。
|
|
|
|
|
* @param callback 日期时间改变监听器
|
|
|
|
|
*/
|
|
|
|
|
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
|
|
|
|
|
mOnDateTimeChangedListener = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通知日期时间改变。
|
|
|
|
|
*/
|
|
|
|
|
private void onDateTimeChanged() {
|
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
|