|
|
@ -14,71 +14,77 @@
|
|
|
|
* limitations under the License.
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
package net.micode.notes.ui; // 定义了类的包名
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.DateFormatSymbols;
|
|
|
|
import java.text.DateFormatSymbols; // 用于日期格式化的符号
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Calendar; // 用于日期和时间操作的类
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
import net.micode.notes.R; // 导入项目的资源文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context; // Android的上下文对象,用于访问应用程序的资源和类
|
|
|
|
|
|
|
|
import android.text.format.DateFormat; // 用于格式化日期和时间
|
|
|
|
|
|
|
|
import android.view.View; // 用于定义和操作视图的类
|
|
|
|
|
|
|
|
import android.widget.FrameLayout; // 用于布局的类,可以包含其他视图
|
|
|
|
|
|
|
|
import android.widget.NumberPicker; // 用于数字选择的控件
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
// DateTimePicker类继承自FrameLayout,用于选择日期和时间
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
|
|
|
import android.widget.NumberPicker;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_HALF_DAY = 12;
|
|
|
|
private static final int HOURS_IN_ALL_DAY = 24; // 一天的小时数
|
|
|
|
private static final int HOURS_IN_ALL_DAY = 24;
|
|
|
|
private static final int DAYS_IN_ALL_WEEK = 7; // 一周的天数
|
|
|
|
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;
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0; // 24小时制下小时选择器的最小值
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23; // 24小时制下小时选择器的最大值
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1; // 12小时制下小时选择器的最小值
|
|
|
|
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
|
|
|
|
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12; // 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;
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0; // AM/PM选择器的最小值
|
|
|
|
private static final int AMPM_SPINNER_MIN_VAL = 0;
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1; // AM/PM选择器的最大值
|
|
|
|
private static final int AMPM_SPINNER_MAX_VAL = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// 定义NumberPicker控件
|
|
|
|
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; // 用于存储日期和时间的Calendar对象
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 用于显示日期的字符串数组
|
|
|
|
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 mIsEnabled = DEFAULT_ENABLE_STATE;
|
|
|
|
private boolean mInitialising; // 是否正在初始化
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mInitialising;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义日期时间改变监听器接口
|
|
|
|
private OnDateTimeChangedListener mOnDateTimeChangedListener;
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// 当日期改变时,更新Calendar对象和日期选择器
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// 当小时改变时,更新Calendar对象和小时选择器
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
boolean isDateChanged = false;
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
|
|
|
// 处理12小时制的情况
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
|
|
|
|
// 如果从PM切换到AM,日期减1
|
|
|
|
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);
|
|
|
@ -88,12 +94,14 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
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 {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 处理24小时制的情况
|
|
|
|
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);
|
|
|
@ -104,6 +112,7 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
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();
|
|
|
@ -115,9 +124,11 @@ 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) {
|
|
|
|
|
|
|
|
// 当分钟改变时,更新Calendar对象和分钟选择器
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
int minValue = mMinuteSpinner.getMinValue();
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
int maxValue = mMinuteSpinner.getMaxValue();
|
|
|
|
int offset = 0;
|
|
|
|
int offset = 0;
|
|
|
@ -144,9 +155,11 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 当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改变时,更新Calendar对象和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);
|
|
|
@ -157,329 +170,357 @@ public class DateTimePicker extends FrameLayout {
|
|
|
|
onDateTimeChanged();
|
|
|
|
onDateTimeChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
public interface OnDateTimeChangedListener {
|
|
|
|
void onDateTimeChanged(DateTimePicker view, int year, int month,
|
|
|
|
// 定义一个接口,用于通知日期时间改变的监听器
|
|
|
|
int dayOfMonth, int hourOfDay, int minute);
|
|
|
|
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) {
|
|
|
|
// DateTimePicker类的构造函数,接收一个Context对象
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context));
|
|
|
|
public DateTimePicker(Context context) {
|
|
|
|
}
|
|
|
|
this(context, System.currentTimeMillis()); // 调用另一个构造函数,传入当前时间
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
// DateTimePicker类的构造函数,接收一个Context对象和一个时间戳
|
|
|
|
super(context);
|
|
|
|
public DateTimePicker(Context context, long date) {
|
|
|
|
mDate = Calendar.getInstance();
|
|
|
|
this(context, date, DateFormat.is24HourFormat(context)); // 调用另一个构造函数,传入是否为24小时制
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set to current time
|
|
|
|
|
|
|
|
setCurrentDate(date);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set the content descriptions
|
|
|
|
|
|
|
|
mInitialising = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
// DateTimePicker类的构造函数,接收一个Context对象、一个时间戳和一个布尔值表示是否为24小时制
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
public DateTimePicker(Context context, long date, boolean is24HourView) {
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
super(context); // 调用父类的构造函数
|
|
|
|
return;
|
|
|
|
mDate = Calendar.getInstance(); // 获取当前日期和时间
|
|
|
|
}
|
|
|
|
mInitialising = true; // 设置初始化标志为true
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; // 判断当前时间是否为AM
|
|
|
|
mDateSpinner.setEnabled(enabled);
|
|
|
|
|
|
|
|
mMinuteSpinner.setEnabled(enabled);
|
|
|
|
// inflate布局,将XML布局文件转换成View对象
|
|
|
|
mHourSpinner.setEnabled(enabled);
|
|
|
|
inflate(context, R.layout.datetime_picker, this);
|
|
|
|
mAmPmSpinner.setEnabled(enabled);
|
|
|
|
|
|
|
|
mIsEnabled = enabled;
|
|
|
|
// 初始化日期选择器,并设置监听器
|
|
|
|
}
|
|
|
|
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(); // 获取AM/PM的字符串数组
|
|
|
|
|
|
|
|
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
|
|
|
|
|
|
|
|
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
|
|
|
|
|
|
|
|
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
|
|
|
|
|
|
|
|
mAmPmSpinner.setDisplayedValues(stringsForAmPm); // 设置显示的值
|
|
|
|
|
|
|
|
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新控件到初始状态
|
|
|
|
|
|
|
|
updateDateControl();
|
|
|
|
|
|
|
|
updateHourControl();
|
|
|
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置是否为24小时制
|
|
|
|
|
|
|
|
set24HourView(is24HourView);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置当前时间
|
|
|
|
|
|
|
|
setCurrentDate(date);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置控件的启用状态
|
|
|
|
|
|
|
|
setEnabled(isEnabled());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置内容描述
|
|
|
|
|
|
|
|
mInitialising = false; // 设置初始化标志为false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
return mIsEnabled;
|
|
|
|
// 设置控件的启用状态
|
|
|
|
|
|
|
|
if (mIsEnabled == enabled) {
|
|
|
|
|
|
|
|
return; // 如果已经是这个状态,则不进行操作
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.setEnabled(enabled); // 调用父类的方法设置启用状态
|
|
|
|
|
|
|
|
mDateSpinner.setEnabled(enabled); // 设置日期选择器的启用状态
|
|
|
|
|
|
|
|
mMinuteSpinner.setEnabled(enabled); // 设置分钟选择器的启用状态
|
|
|
|
|
|
|
|
mHourSpinner.setEnabled(enabled); // 设置小时选择器的启用状态
|
|
|
|
|
|
|
|
mAmPmSpinner.setEnabled(enabled); // 设置AM/PM选择器的启用状态
|
|
|
|
|
|
|
|
mIsEnabled = enabled; // 更新启用状态标志
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@Override
|
|
|
|
* Get the current date in millis
|
|
|
|
public boolean isEnabled() {
|
|
|
|
*
|
|
|
|
// 返回控件的启用状态
|
|
|
|
* @return the current date in millis
|
|
|
|
return mIsEnabled;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set the current date
|
|
|
|
* Get the current date in millis
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param date The current date in millis
|
|
|
|
* @return the current date in millis
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentDate(long date) {
|
|
|
|
public long getCurrentDateInTimeMillis() {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
// 获取当前日期的时间戳
|
|
|
|
cal.setTimeInMillis(date);
|
|
|
|
return mDate.getTimeInMillis();
|
|
|
|
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
|
|
|
|
* Set the current date
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param year The current year
|
|
|
|
* @param year The current year
|
|
|
|
* @param month The current month
|
|
|
|
* @param month The current month
|
|
|
|
* @param dayOfMonth The current dayOfMonth
|
|
|
|
* @param dayOfMonth The current dayOfMonth
|
|
|
|
* @param hourOfDay The current hourOfDay
|
|
|
|
* @param hourOfDay The current hourOfDay
|
|
|
|
* @param minute The current minute
|
|
|
|
* @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) {
|
|
|
|
setCurrentYear(year);
|
|
|
|
// 设置当前日期和时间
|
|
|
|
setCurrentMonth(month);
|
|
|
|
setCurrentYear(year); // 设置年份
|
|
|
|
setCurrentDay(dayOfMonth);
|
|
|
|
setCurrentMonth(month); // 设置月份
|
|
|
|
setCurrentHour(hourOfDay);
|
|
|
|
setCurrentDay(dayOfMonth); // 设置日期
|
|
|
|
setCurrentMinute(minute);
|
|
|
|
setCurrentHour(hourOfDay); // 设置小时
|
|
|
|
}
|
|
|
|
setCurrentMinute(minute); // 设置分钟
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get current year
|
|
|
|
* Get current year
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return The current year
|
|
|
|
* @return The current year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentYear() {
|
|
|
|
public int getCurrentYear() {
|
|
|
|
return mDate.get(Calendar.YEAR);
|
|
|
|
// 获取当前年份
|
|
|
|
}
|
|
|
|
return mDate.get(Calendar.YEAR); // 从mDate中获取年份
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set current year
|
|
|
|
* Set current year
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param year The current year
|
|
|
|
* @param year The current year
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
public void setCurrentYear(int year) {
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
// 设置当前年份
|
|
|
|
return;
|
|
|
|
if (!mInitialising && year == getCurrentYear()) {
|
|
|
|
}
|
|
|
|
return; // 如果不是初始化状态且年份未改变,则返回
|
|
|
|
mDate.set(Calendar.YEAR, year);
|
|
|
|
|
|
|
|
updateDateControl();
|
|
|
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDate.set(Calendar.YEAR, year); // 更新mDate中的年份
|
|
|
|
|
|
|
|
updateDateControl(); // 更新日期控件的显示
|
|
|
|
|
|
|
|
onDateTimeChanged(); // 通知日期时间已改变
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get current month in the year
|
|
|
|
* Get current month in the year
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return The 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); // 从mDate中获取月份
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set current month in the year
|
|
|
|
* Set current month in the year
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param month The 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()) {
|
|
|
|
// 设置当前月份
|
|
|
|
return;
|
|
|
|
if (!mInitialising && month == getCurrentMonth()) {
|
|
|
|
}
|
|
|
|
return; // 如果不是初始化状态且月份未改变,则返回
|
|
|
|
mDate.set(Calendar.MONTH, month);
|
|
|
|
|
|
|
|
updateDateControl();
|
|
|
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDate.set(Calendar.MONTH, month); // 更新mDate中的月份
|
|
|
|
|
|
|
|
updateDateControl(); // 更新日期控件的显示
|
|
|
|
|
|
|
|
onDateTimeChanged(); // 通知日期时间已改变
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get current day of the month
|
|
|
|
* Get current day of the month
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return The 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); // 从mDate中获取日期
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set current day of the month
|
|
|
|
* Set current day of the month
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param dayOfMonth The 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()) {
|
|
|
|
// 设置当前日期
|
|
|
|
return;
|
|
|
|
if (!mInitialising && dayOfMonth == getCurrentDay()) {
|
|
|
|
}
|
|
|
|
return; // 如果不是初始化状态且日期未改变,则返回
|
|
|
|
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
|
|
|
|
|
|
|
updateDateControl();
|
|
|
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); // 更新mDate中的日期
|
|
|
|
|
|
|
|
updateDateControl(); // 更新日期控件的显示
|
|
|
|
|
|
|
|
onDateTimeChanged(); // 通知日期时间已改变
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get current hour in 24 hour mode, in the range (0~23)
|
|
|
|
* Get current hour in 24 hour mode, in the range (0~23)
|
|
|
|
* @return The current hour in 24 hour mode
|
|
|
|
* @return The current hour in 24 hour mode
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
public int getCurrentHourOfDay() {
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY);
|
|
|
|
// 获取当前小时(24小时制),范围为0到23
|
|
|
|
}
|
|
|
|
return mDate.get(Calendar.HOUR_OF_DAY); // 从mDate中获取小时
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getCurrentHour() {
|
|
|
|
private int getCurrentHour() {
|
|
|
|
if (mIs24HourView){
|
|
|
|
// 获取当前小时,根据是否为24小时制返回不同的值
|
|
|
|
return getCurrentHourOfDay();
|
|
|
|
if (mIs24HourView) {
|
|
|
|
|
|
|
|
return getCurrentHourOfDay(); // 如果是24小时制,直接返回当前小时
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
int hour = getCurrentHourOfDay(); // 获取当前小时
|
|
|
|
|
|
|
|
// 如果小时大于12,返回小时减去12(转换为12小时制)
|
|
|
|
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
|
|
|
|
return hour - HOURS_IN_HALF_DAY;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
// 如果小时为0,返回12;否则返回当前小时
|
|
|
|
if (hour > HOURS_IN_HALF_DAY) {
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
|
|
|
|
return hour - HOURS_IN_HALF_DAY;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
* Set current hour in 24 hour mode, in the range (0~23)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param hourOfDay
|
|
|
|
* @param hourOfDay 当前小时(24小时制)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
public void setCurrentHour(int hourOfDay) {
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
// 设置当前小时
|
|
|
|
return;
|
|
|
|
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
|
|
|
|
}
|
|
|
|
return; // 如果不是初始化状态且小时未改变,则返回
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
|
|
|
}
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); // 更新mDate中的小时
|
|
|
|
if (hourOfDay >= HOURS_IN_HALF_DAY) {
|
|
|
|
if (!mIs24HourView) {
|
|
|
|
mIsAm = false;
|
|
|
|
// 如果不是24小时制,更新AM/PM状态
|
|
|
|
if (hourOfDay > HOURS_IN_HALF_DAY) {
|
|
|
|
if (hourOfDay >= HOURS_IN_HALF_DAY) {
|
|
|
|
hourOfDay -= HOURS_IN_HALF_DAY;
|
|
|
|
mIsAm = false; // 设置为PM
|
|
|
|
}
|
|
|
|
if (hourOfDay > HOURS_IN_HALF_DAY) {
|
|
|
|
} else {
|
|
|
|
hourOfDay -= HOURS_IN_HALF_DAY; // 转换为12小时制
|
|
|
|
mIsAm = true;
|
|
|
|
}
|
|
|
|
if (hourOfDay == 0) {
|
|
|
|
} else {
|
|
|
|
hourOfDay = HOURS_IN_HALF_DAY;
|
|
|
|
mIsAm = true; // 设置为AM
|
|
|
|
}
|
|
|
|
if (hourOfDay == 0) {
|
|
|
|
|
|
|
|
hourOfDay = HOURS_IN_HALF_DAY; // 如果小时为0,设置为12
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateAmPmControl();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mHourSpinner.setValue(hourOfDay);
|
|
|
|
updateAmPmControl(); // 更新AM/PM控件
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mHourSpinner.setValue(hourOfDay); // 更新小时选择器的值
|
|
|
|
|
|
|
|
onDateTimeChanged(); // 通知日期时间已改变
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get currentMinute
|
|
|
|
* Get currentMinute
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return The Current Minute
|
|
|
|
* @return The Current Minute
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
public int getCurrentMinute() {
|
|
|
|
return mDate.get(Calendar.MINUTE);
|
|
|
|
// 获取当前分钟
|
|
|
|
}
|
|
|
|
return mDate.get(Calendar.MINUTE); // 从mDate中获取分钟
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set current minute
|
|
|
|
* Set current minute
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
public void setCurrentMinute(int minute) {
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
|
// 设置当前分钟
|
|
|
|
return;
|
|
|
|
if (!mInitialising && minute == getCurrentMinute()) {
|
|
|
|
}
|
|
|
|
return; // 如果不是初始化状态且分钟未改变,则返回
|
|
|
|
mMinuteSpinner.setValue(minute);
|
|
|
|
|
|
|
|
mDate.set(Calendar.MINUTE, minute);
|
|
|
|
|
|
|
|
onDateTimeChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mMinuteSpinner.setValue(minute); // 更新分钟选择器的值
|
|
|
|
|
|
|
|
mDate.set(Calendar.MINUTE, minute); // 更新mDate中的分钟
|
|
|
|
|
|
|
|
onDateTimeChanged(); // 通知日期时间已改变
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @return true if this is in 24 hour view else false.
|
|
|
|
* @return true if this is in 24 hour view else false.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public boolean is24HourView () {
|
|
|
|
public boolean is24HourView() {
|
|
|
|
return mIs24HourView;
|
|
|
|
// 返回当前是否为24小时制
|
|
|
|
}
|
|
|
|
return mIs24HourView; // 返回24小时制状态
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set whether in 24 hour or AM/PM mode.
|
|
|
|
* Set whether in 24 hour or AM/PM mode.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param is24HourView True for 24 hour mode. False for 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) {
|
|
|
|
// 设置是否为24小时制或AM/PM模式
|
|
|
|
return;
|
|
|
|
if (mIs24HourView == is24HourView) {
|
|
|
|
}
|
|
|
|
return; // 如果当前状态与传入状态相同,则返回
|
|
|
|
mIs24HourView = is24HourView;
|
|
|
|
|
|
|
|
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
|
|
|
|
|
|
|
|
int hour = getCurrentHourOfDay();
|
|
|
|
|
|
|
|
updateHourControl();
|
|
|
|
|
|
|
|
setCurrentHour(hour);
|
|
|
|
|
|
|
|
updateAmPmControl();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mIs24HourView = is24HourView; // 更新24小时制状态
|
|
|
|
|
|
|
|
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE); // 根据模式设置AM/PM选择器的可见性
|
|
|
|
|
|
|
|
int hour = getCurrentHourOfDay(); // 获取当前小时
|
|
|
|
|
|
|
|
updateHourControl(); // 更新小时控件
|
|
|
|
|
|
|
|
setCurrentHour(hour); // 设置当前小时
|
|
|
|
|
|
|
|
updateAmPmControl(); // 更新AM/PM控件
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateDateControl() {
|
|
|
|
private void updateDateControl() {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
// 更新日期选择器的显示值
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis());
|
|
|
|
Calendar cal = Calendar.getInstance(); // 获取当前时间
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
|
|
|
|
cal.setTimeInMillis(mDate.getTimeInMillis()); // 设置为当前日期
|
|
|
|
mDateSpinner.setDisplayedValues(null);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1); // 调整为显示的起始日期
|
|
|
|
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
|
|
|
|
mDateSpinner.setDisplayedValues(null); // 清空显示值
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
// 填充日期显示值
|
|
|
|
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
|
|
|
|
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
|
|
|
|
}
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, 1); // 增加一天
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues);
|
|
|
|
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); // 格式化日期
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
|
|
|
|
|
|
|
|
mDateSpinner.invalidate();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDateSpinner.setDisplayedValues(mDateDisplayValues); // 设置日期选择器的显示值
|
|
|
|
|
|
|
|
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); // 设置选择器的当前值为中间值
|
|
|
|
|
|
|
|
mDateSpinner.invalidate(); // 刷新选择器
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
private void updateAmPmControl() {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
// 更新AM/PM选择器的显示
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE);
|
|
|
|
if (mIs24HourView) {
|
|
|
|
} else {
|
|
|
|
mAmPmSpinner.setVisibility(View.GONE); // 隐藏AM/PM选择器
|
|
|
|
int index = mIsAm ? Calendar.AM : Calendar.PM;
|
|
|
|
} else {
|
|
|
|
mAmPmSpinner.setValue(index);
|
|
|
|
int index = mIsAm ? Calendar.AM : Calendar.PM; // 根据当前状态设置索引
|
|
|
|
mAmPmSpinner.setVisibility(View.VISIBLE);
|
|
|
|
mAmPmSpinner.setValue(index); // 设置AM/PM选择器的值
|
|
|
|
}
|
|
|
|
mAmPmSpinner.setVisibility(View.VISIBLE); // 显示AM/PM选择器
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateHourControl() {
|
|
|
|
private void updateHourControl() {
|
|
|
|
if (mIs24HourView) {
|
|
|
|
// 更新小时选择器的范围
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
|
|
|
|
if (mIs24HourView) {
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); // 设置24小时制下的最小值
|
|
|
|
} else {
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW); // 设置24小时制下的最大值
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
|
|
|
|
} else {
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
|
|
|
|
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW); // 设置12小时制下的最小值
|
|
|
|
}
|
|
|
|
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW); // 设置12小时制下的最大值
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set the callback that indicates the 'Set' button has been pressed.
|
|
|
|
* Set the callback that indicates the 'Set' button has been pressed.
|
|
|
|
* @param callback the callback, if null will do nothing
|
|
|
|
* @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) {
|
|
|
|
// 通知日期时间已改变
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
if (mOnDateTimeChangedListener != null) {
|
|
|
|
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
|
|
|
|
// 如果监听器不为空,调用其方法
|
|
|
|
}
|
|
|
|
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
|
|
|
|
|
|
|
|
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|