You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Dome/src1/net/micode/notes/ui/DateTimePicker.java

631 lines
23 KiB

3 weeks ago
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.ui;
import java.text.DateFormatSymbols;
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;
public class DateTimePicker extends 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;
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;
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
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 MINUT_SPINNER_MIN_VAL = 0;
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;
private Calendar mDate;
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
private boolean mIsAm;
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) {
1 week ago
// 将日期增加或减少指定的天数
3 weeks ago
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
1 week ago
// 更新日期控件
3 weeks ago
updateDateControl();
1 week ago
// 日期时间改变时调用
3 weeks ago
onDateTimeChanged();
}
};
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
1 week ago
// 判断是否日期改变
3 weeks ago
boolean isDateChanged = false;
1 week ago
// 获取当前时间
3 weeks ago
Calendar cal = Calendar.getInstance();
1 week ago
// 判断是否为24小时制
3 weeks ago
if (!mIs24HourView) {
1 week ago
// 判断是否为上午
3 weeks ago
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
1 week ago
// 如果当前时间是上午11点并且选择的时间是下午12点则日期加1天
3 weeks ago
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true;
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
1 week ago
// 如果当前时间是下午12点并且选择的时间是上午11点则日期减1天
3 weeks ago
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true;
}
1 week ago
// 判断是否跨过12点
3 weeks ago
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
1 week ago
// 跨过12点则上午和下午互换
3 weeks ago
mIsAm = !mIsAm;
updateAmPmControl();
}
} else {
1 week ago
// 判断是否跨过24点
3 weeks ago
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
1 week ago
// 如果当前时间是23点并且选择的时间是0点则日期加1天
3 weeks ago
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1);
isDateChanged = true;
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
1 week ago
// 如果当前时间是0点并且选择的时间是23点则日期减1天
3 weeks ago
cal.setTimeInMillis(mDate.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, -1);
isDateChanged = true;
}
}
1 week ago
// 计算新的小时数
3 weeks ago
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
1 week ago
// 设置新的小时数
3 weeks ago
mDate.set(Calendar.HOUR_OF_DAY, newHour);
1 week ago
// 通知时间改变
3 weeks ago
onDateTimeChanged();
1 week ago
// 如果日期改变,则更新日期
3 weeks ago
if (isDateChanged) {
setCurrentYear(cal.get(Calendar.YEAR));
setCurrentMonth(cal.get(Calendar.MONTH));
setCurrentDay(cal.get(Calendar.DAY_OF_MONTH));
}
}
};
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
1 week ago
// 获取分钟选择器的最小值和最大值
3 weeks ago
int minValue = mMinuteSpinner.getMinValue();
int maxValue = mMinuteSpinner.getMaxValue();
int offset = 0;
1 week ago
// 如果旧值等于最大值且新值等于最小值则偏移量为1
3 weeks ago
if (oldVal == maxValue && newVal == minValue) {
offset += 1;
1 week ago
// 如果旧值等于最小值且新值等于最大值,则偏移量为-1
3 weeks ago
} else if (oldVal == minValue && newVal == maxValue) {
offset -= 1;
}
1 week ago
// 如果偏移量不为0则更新日期和时间
3 weeks ago
if (offset != 0) {
mDate.add(Calendar.HOUR_OF_DAY, offset);
mHourSpinner.setValue(getCurrentHour());
updateDateControl();
int newHour = getCurrentHourOfDay();
1 week ago
// 如果新小时数大于等于半天的小时数,则更新上午/下午控制
3 weeks ago
if (newHour >= HOURS_IN_HALF_DAY) {
mIsAm = false;
updateAmPmControl();
1 week ago
// 否则,更新上午/下午控制
3 weeks ago
} else {
mIsAm = true;
updateAmPmControl();
}
}
1 week ago
// 设置新分钟值
3 weeks ago
mDate.set(Calendar.MINUTE, newVal);
1 week ago
// 日期时间改变
3 weeks ago
onDateTimeChanged();
}
};
1 week ago
// 定义一个NumberPicker的值改变监听器
3 weeks ago
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
1 week ago
// 切换AM/PM
3 weeks ago
mIsAm = !mIsAm;
1 week ago
// 如果是AM则将时间减去12小时
3 weeks ago
if (mIsAm) {
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
1 week ago
// 如果是PM则将时间加上12小时
3 weeks ago
} else {
mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY);
}
1 week ago
// 更新AM/PM控制
3 weeks ago
updateAmPmControl();
1 week ago
// 日期时间改变
3 weeks ago
onDateTimeChanged();
}
};
public interface OnDateTimeChangedListener {
1 week ago
// 当日期时间改变时调用的方法
3 weeks ago
void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute);
}
1 week ago
// 构造方法,使用当前时间作为日期时间
3 weeks ago
public DateTimePicker(Context context) {
this(context, System.currentTimeMillis());
}
1 week ago
// 构造方法,使用指定时间作为日期时间
3 weeks ago
public DateTimePicker(Context context, long date) {
this(context, date, DateFormat.is24HourFormat(context));
}
public DateTimePicker(Context context, long date, boolean is24HourView) {
super(context);
1 week ago
// 获取当前日期
3 weeks ago
mDate = Calendar.getInstance();
mInitialising = true;
1 week ago
// 判断当前小时是否大于等于12
3 weeks ago
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
1 week ago
// 加载布局文件
3 weeks ago
inflate(context, R.layout.datetime_picker, this);
1 week ago
// 获取日期选择器
3 weeks ago
mDateSpinner = (NumberPicker) findViewById(R.id.date);
1 week ago
// 设置日期选择器的最小值
3 weeks ago
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
1 week ago
// 设置日期选择器的最大值
3 weeks ago
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
1 week ago
// 设置日期选择器的值改变监听器
3 weeks ago
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
1 week ago
// 获取小时选择器
3 weeks ago
mHourSpinner = (NumberPicker) findViewById(R.id.hour);
1 week ago
// 设置小时选择器的值改变监听器
3 weeks ago
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
1 week ago
// 获取分钟选择器
3 weeks ago
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
1 week ago
// 设置分钟选择器的最小值
3 weeks ago
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
1 week ago
// 设置分钟选择器的最大值
3 weeks ago
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
1 week ago
// 设置分钟选择器的长按更新间隔
3 weeks ago
mMinuteSpinner.setOnLongPressUpdateInterval(100);
1 week ago
// 设置分钟选择器的值改变监听器
3 weeks ago
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
1 week ago
// 获取上午下午选择器
3 weeks ago
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
1 week ago
// 设置上午下午选择器的最小值
3 weeks ago
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
1 week ago
// 设置上午下午选择器的最大值
3 weeks ago
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
1 week ago
// 设置上午下午选择器的显示值
3 weeks ago
mAmPmSpinner.setDisplayedValues(stringsForAmPm);
1 week ago
// 设置上午下午选择器的值改变监听器
3 weeks ago
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
1 week ago
// 重写setEnabled方法
3 weeks ago
public void setEnabled(boolean enabled) {
1 week ago
// 如果当前状态和传入的状态相同,则直接返回
3 weeks ago
if (mIsEnabled == enabled) {
return;
}
1 week ago
// 调用父类的setEnabled方法
3 weeks ago
super.setEnabled(enabled);
1 week ago
// 设置日期选择器是否可用
3 weeks ago
mDateSpinner.setEnabled(enabled);
1 week ago
// 设置分钟选择器是否可用
3 weeks ago
mMinuteSpinner.setEnabled(enabled);
1 week ago
// 设置小时选择器是否可用
3 weeks ago
mHourSpinner.setEnabled(enabled);
1 week ago
// 设置上午/下午选择器是否可用
3 weeks ago
mAmPmSpinner.setEnabled(enabled);
1 week ago
// 更新当前状态
3 weeks ago
mIsEnabled = enabled;
}
@Override
public boolean isEnabled() {
1 week ago
// 返回mIsEnabled的值
3 weeks ago
return mIsEnabled;
}
/**
* Get the current date in millis
*
* @return the current date in millis
*/
public long getCurrentDateInTimeMillis() {
return mDate.getTimeInMillis();
}
/**
* Set the current date
*
* @param date The current date in millis
*/
public void setCurrentDate(long date) {
1 week ago
// 获取当前日期
3 weeks ago
Calendar cal = Calendar.getInstance();
1 week ago
// 将传入的日期转换为毫秒
3 weeks ago
cal.setTimeInMillis(date);
1 week ago
// 设置当前日期
3 weeks ago
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
*
* @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,
int dayOfMonth, int hourOfDay, int minute) {
1 week ago
// 设置当前年份
3 weeks ago
setCurrentYear(year);
1 week ago
// 设置当前月份
3 weeks ago
setCurrentMonth(month);
1 week ago
// 设置当前日期
3 weeks ago
setCurrentDay(dayOfMonth);
1 week ago
// 设置当前小时
3 weeks ago
setCurrentHour(hourOfDay);
1 week ago
// 设置当前分钟
3 weeks ago
setCurrentMinute(minute);
}
/**
* Get current year
*
* @return The current year
*/
public int getCurrentYear() {
1 week ago
// 获取当前年份
3 weeks ago
return mDate.get(Calendar.YEAR);
}
/**
* Set current year
*
* @param year The current year
*/
public void setCurrentYear(int year) {
1 week ago
// 如果当前年份与传入的年份相同,则不进行任何操作
3 weeks ago
if (!mInitialising && year == getCurrentYear()) {
return;
}
1 week ago
// 设置当前年份
3 weeks ago
mDate.set(Calendar.YEAR, year);
1 week ago
// 更新日期控件
3 weeks ago
updateDateControl();
1 week ago
// 日期时间改变
3 weeks ago
onDateTimeChanged();
}
/**
* Get current month in the year
*
* @return The current month in the year
*/
public int getCurrentMonth() {
1 week ago
// 获取当前月份
3 weeks ago
return mDate.get(Calendar.MONTH);
}
/**
* Set current month in the year
*
* @param month The month in the year
*/
public void setCurrentMonth(int month) {
1 week ago
// 如果当前月份和要设置的月份相同,则不进行设置
3 weeks ago
if (!mInitialising && month == getCurrentMonth()) {
return;
}
1 week ago
// 设置当前月份
3 weeks ago
mDate.set(Calendar.MONTH, month);
1 week ago
// 更新日期控件
3 weeks ago
updateDateControl();
1 week ago
// 日期改变回调
3 weeks ago
onDateTimeChanged();
}
/**
* Get current day of the month
*
* @return The day of the month
*/
1 week ago
// 获取当前日期
3 weeks ago
public int getCurrentDay() {
1 week ago
// 获取当前日期的日
3 weeks ago
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) {
1 week ago
// 如果当前不在初始化状态,并且传入的日期与当前日期相同,则直接返回
3 weeks ago
if (!mInitialising && dayOfMonth == getCurrentDay()) {
return;
}
1 week ago
// 设置当前日期
3 weeks ago
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
1 week ago
// 更新日期控件
3 weeks ago
updateDateControl();
1 week ago
// 日期改变时触发
3 weeks ago
onDateTimeChanged();
}
/**
* Get current hour in 24 hour mode, in the range (0~23)
* @return The current hour in 24 hour mode
*/
1 week ago
// 获取当前小时
3 weeks ago
public int getCurrentHourOfDay() {
return mDate.get(Calendar.HOUR_OF_DAY);
}
1 week ago
// 获取当前小时
3 weeks ago
private int getCurrentHour() {
1 week ago
// 如果是24小时制
3 weeks ago
if (mIs24HourView){
1 week ago
// 获取当前小时
3 weeks ago
return getCurrentHourOfDay();
} else {
1 week ago
// 获取当前小时
3 weeks ago
int hour = getCurrentHourOfDay();
1 week ago
// 如果当前小时大于12
3 weeks ago
if (hour > HOURS_IN_HALF_DAY) {
1 week ago
// 返回当前小时减去12
3 weeks ago
return hour - HOURS_IN_HALF_DAY;
} else {
1 week ago
// 如果当前小时等于0返回12
3 weeks ago
return hour == 0 ? HOURS_IN_HALF_DAY : hour;
}
}
}
/**
* Set current hour in 24 hour mode, in the range (0~23)
*
* @param hourOfDay
*/
1 week ago
// 设置当前小时
3 weeks ago
public void setCurrentHour(int hourOfDay) {
1 week ago
// 如果当前正在初始化,或者当前小时和要设置的小时相同,则直接返回
3 weeks ago
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
return;
}
1 week ago
// 设置当前小时
3 weeks ago
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
1 week ago
// 如果不是24小时制
3 weeks ago
if (!mIs24HourView) {
1 week ago
// 如果当前小时大于等于12
3 weeks ago
if (hourOfDay >= HOURS_IN_HALF_DAY) {
1 week ago
// 设置为下午
3 weeks ago
mIsAm = false;
1 week ago
// 如果当前小时大于12
3 weeks ago
if (hourOfDay > HOURS_IN_HALF_DAY) {
1 week ago
// 当前小时减去12
3 weeks ago
hourOfDay -= HOURS_IN_HALF_DAY;
}
} else {
1 week ago
// 设置为上午
3 weeks ago
mIsAm = true;
1 week ago
// 如果当前小时为0
3 weeks ago
if (hourOfDay == 0) {
1 week ago
// 当前小时设置为12
3 weeks ago
hourOfDay = HOURS_IN_HALF_DAY;
}
}
1 week ago
// 更新上午/下午控制
3 weeks ago
updateAmPmControl();
}
1 week ago
// 设置小时选择器的值
3 weeks ago
mHourSpinner.setValue(hourOfDay);
1 week ago
// 日期时间改变
3 weeks ago
onDateTimeChanged();
}
/**
* Get currentMinute
*
* @return The Current Minute
*/
1 week ago
// 获取当前分钟
3 weeks ago
public int getCurrentMinute() {
return mDate.get(Calendar.MINUTE);
}
/**
* Set current minute
*/
public void setCurrentMinute(int minute) {
1 week ago
// 如果mInitialising为false且minute等于getCurrentMinute(),则直接返回
3 weeks ago
if (!mInitialising && minute == getCurrentMinute()) {
return;
}
1 week ago
// 设置mMinuteSpinner的值为minute
3 weeks ago
mMinuteSpinner.setValue(minute);
1 week ago
// 设置mDate的分钟为minute
3 weeks ago
mDate.set(Calendar.MINUTE, minute);
1 week ago
// 调用onDateTimeChanged()方法
3 weeks ago
onDateTimeChanged();
}
/**
* @return true if this is in 24 hour view else false.
*/
1 week ago
// 判断是否为24小时制
3 weeks ago
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.
*/
public void set24HourView(boolean is24HourView) {
1 week ago
// 如果当前时间格式和传入的时间格式相同,则直接返回
3 weeks ago
if (mIs24HourView == is24HourView) {
return;
}
1 week ago
// 更新时间格式
3 weeks ago
mIs24HourView = is24HourView;
1 week ago
// 如果是24小时制则隐藏上午/下午选择器
3 weeks ago
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
1 week ago
// 获取当前小时数
3 weeks ago
int hour = getCurrentHourOfDay();
1 week ago
// 更新小时选择器
3 weeks ago
updateHourControl();
1 week ago
// 设置当前小时数
3 weeks ago
setCurrentHour(hour);
1 week ago
// 更新上午/下午选择器
3 weeks ago
updateAmPmControl();
}
private void updateDateControl() {
1 week ago
// 获取当前日期
3 weeks ago
Calendar cal = Calendar.getInstance();
1 week ago
// 将当前日期设置为mDate的时间
3 weeks ago
cal.setTimeInMillis(mDate.getTimeInMillis());
1 week ago
// 将当前日期向前推DAYS_IN_ALL_WEEK / 2 - 1天
3 weeks ago
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
1 week ago
// 清空mDateSpinner的显示值
3 weeks ago
mDateSpinner.setDisplayedValues(null);
1 week ago
// 循环DAYS_IN_ALL_WEEK次
3 weeks ago
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
1 week ago
// 将当前日期加1天
3 weeks ago
cal.add(Calendar.DAY_OF_YEAR, 1);
1 week ago
// 将当前日期格式化为"MM.dd EEEE"的字符串
3 weeks ago
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
}
1 week ago
// 将mDateSpinner的显示值设置为mDateDisplayValues
3 weeks ago
mDateSpinner.setDisplayedValues(mDateDisplayValues);
1 week ago
// 将mDateSpinner的值设置为DAYS_IN_ALL_WEEK / 2
3 weeks ago
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
1 week ago
// 使mDateSpinner无效
3 weeks ago
mDateSpinner.invalidate();
}
private void updateAmPmControl() {
1 week ago
// 如果是24小时制则隐藏上午/下午选择器
3 weeks ago
if (mIs24HourView) {
mAmPmSpinner.setVisibility(View.GONE);
} else {
1 week ago
// 如果是上午,则选择上午,否则选择下午
3 weeks ago
int index = mIsAm ? Calendar.AM : Calendar.PM;
mAmPmSpinner.setValue(index);
1 week ago
// 显示上午/下午选择器
3 weeks ago
mAmPmSpinner.setVisibility(View.VISIBLE);
}
}
1 week ago
// 更新小时控件
3 weeks ago
private void updateHourControl() {
1 week ago
// 如果是24小时制
3 weeks ago
if (mIs24HourView) {
1 week ago
// 设置小时选择器的最小值为24小时制最小值
3 weeks ago
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
1 week ago
// 设置小时选择器的最大值为24小时制最大值
3 weeks ago
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
} else {
1 week ago
// 设置小时选择器的最小值为12小时制最小值
3 weeks ago
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
1 week ago
// 设置小时选择器的最大值为12小时制最大值
3 weeks ago
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
}
}
/**
* Set the callback that indicates the 'Set' button has been pressed.
* @param callback the callback, if null will do nothing
*/
1 week ago
// 设置日期时间改变监听器
3 weeks ago
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
mOnDateTimeChangedListener = callback;
}
1 week ago
// 日期时间改变时调用
3 weeks ago
private void onDateTimeChanged() {
1 week ago
// 如果日期时间改变监听器不为空
3 weeks ago
if (mOnDateTimeChangedListener != null) {
1 week ago
// 调用日期时间改变监听器的onDateTimeChanged方法并传入当前日期时间
3 weeks ago
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
}
}
}