下面列出了怎么用android.widget.DatePicker.OnDateChangedListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sets the current, min, and max values on the given DatePicker and ensures that
* min <= current <= max, adjusting current and max if needed.
*
* @param year The current year to set.
* @param month The current month to set. 0-based.
* @param day The current day to set.
* @param minMillisUtc The minimum allowed date, in milliseconds from the epoch according to a
* proleptic Gregorian calendar (no Julian switch).
* @param maxMillisUtc The maximum allowed date, in milliseconds from the epoch according to a
* proleptic Gregorian calendar (no Julian switch).
*/
public static void normalize(DatePicker picker, final OnDateChangedListener listener,
int year, int month, int day, long minMillisUtc, long maxMillisUtc) {
DateAndMillis currentDate = DateAndMillis.create(year, month, day);
DateAndMillis minDate = DateAndMillis.create(minMillisUtc);
DateAndMillis maxDate = DateAndMillis.create(maxMillisUtc);
// Ensure min <= current <= max, adjusting current and max if needed.
if (maxDate.millisForPicker < minDate.millisForPicker) {
maxDate = minDate;
}
if (currentDate.millisForPicker < minDate.millisForPicker) {
currentDate = minDate;
} else if (currentDate.millisForPicker > maxDate.millisForPicker) {
currentDate = maxDate;
}
setLimits(picker, currentDate.millisForPicker, minDate.millisForPicker,
maxDate.millisForPicker);
picker.init(currentDate.year, currentDate.month, currentDate.day, listener);
}
/**
* Normalizes an existing DateDialogPicker changing the default date if
* needed to comply with the {@code min} and {@code max} attributes.
*/
static void normalize(DatePicker picker, OnDateChangedListener listener,
int year, int month, int day, int hour, int minute, long min, long max) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.clear();
calendar.set(year, month, day, hour, minute, 0);
if (calendar.getTimeInMillis() < min) {
calendar.clear();
calendar.setTimeInMillis(min);
} else if (calendar.getTimeInMillis() > max) {
calendar.clear();
calendar.setTimeInMillis(max);
}
picker.init(
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), listener);
setLimits(picker, min, max);
}
/**
* Normalizes an existing DateDialogPicker changing the default date if
* needed to comply with the {@code min} and {@code max} attributes.
*/
static void normalize(DatePicker picker, OnDateChangedListener listener,
int year, int month, int day, int hour, int minute, long min, long max) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.clear();
calendar.set(year, month, day, hour, minute, 0);
if (calendar.getTimeInMillis() < min) {
calendar.clear();
calendar.setTimeInMillis(min);
} else if (calendar.getTimeInMillis() > max) {
calendar.clear();
calendar.setTimeInMillis(max);
}
picker.init(
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), listener);
setLimits(picker, min, max);
}
/**
* Create and return the user interface view for this fragment.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
int theme = getArguments().getInt("theme");
int initialYear = getArguments().getInt("year");
int initialMonth = getArguments().getInt("month");
int initialDay = getArguments().getInt("day");
Date minDate = (Date) getArguments().getSerializable("minDate");
Date maxDate = (Date) getArguments().getSerializable("maxDate");
// Unless we inflate using a cloned inflater with a Holo theme,
// on Lollipop devices the DatePicker will be the new-style
// DatePicker, which is not what we want. So we will
// clone the inflater that we're given but with our specified
// theme, then inflate the layout with this new inflater.
Context contextThemeWrapper = new ContextThemeWrapper(
getActivity(),
theme == SlideDateTimePicker.HOLO_DARK ?
android.R.style.Theme_Holo :
android.R.style.Theme_Holo_Light);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View v = localInflater.inflate(R.layout.fragment_date, container, false);
mDatePicker = (CustomDatePicker) v.findViewById(R.id.datePicker);
// block keyboard popping up on touch
mDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
mDatePicker.init(
initialYear,
initialMonth,
initialDay,
new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
mCallback.onDateChanged(year, monthOfYear, dayOfMonth);
}
});
if (minDate != null)
mDatePicker.setMinDate(minDate.getTime());
if (maxDate != null)
mDatePicker.setMaxDate(maxDate.getTime());
return v;
}