下面列出了android.app.DatePickerDialog#getDatePicker ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
R.style.CustomDatePickerDialogTheme, this, mParent.mYear, mParent.mMonth, 1);
DatePicker datePicker = dialog.getDatePicker();
// Limit range.
Calendar c = mParent.getCalendar();
datePicker.setMinDate(c.getTimeInMillis());
c.set(Calendar.YEAR, mParent.mYear + CC_EXP_YEARS_COUNT - 1);
datePicker.setMaxDate(c.getTimeInMillis());
// Remove day.
datePicker.findViewById(getResources().getIdentifier("day", "id", "android"))
.setVisibility(View.GONE);
return dialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
R.style.CustomDatePickerDialogTheme, this, mParent.mYear, mParent.mMonth, 1);
DatePicker datePicker = dialog.getDatePicker();
// Limit range.
Calendar c = mParent.getCalendar();
datePicker.setMinDate(c.getTimeInMillis());
c.set(Calendar.YEAR, mParent.mYear + CC_EXP_YEARS_COUNT - 1);
datePicker.setMaxDate(c.getTimeInMillis());
// Remove day.
datePicker.findViewById(getResources().getIdentifier("day", "id", "android"))
.setVisibility(View.GONE);
return dialog;
}
public Dialog createDateSetDialog(String message, Boolean cancelable, OBUtils.RunLambda completionBlock)
{
dateSetCompletionBlock = completionBlock;
final Calendar calendar = Calendar.getInstance();
DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//
d.setCancelable(cancelable);
d.setCanceledOnTouchOutside(cancelable);
//
if (message == null)
{
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCustomTitle(linearLayout);
} else
{
d.setMessage(message + "\n");
}
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, null, (DialogInterface.OnClickListener) null);
//
DatePicker datePicker = d.getDatePicker();
calendar.clear();
calendar.set(2016, Calendar.JANUARY, 1);
datePicker.setMinDate(calendar.getTimeInMillis());
calendar.clear();
calendar.set(2020, Calendar.DECEMBER, 31);
datePicker.setMaxDate(calendar.getTimeInMillis());
return d;
}
void showPickDateDialog (final DatePickerDialog.OnDateSetListener listener, Date startDate)
{
Calendar currentCalendar = Calendar.getInstance();
if (startDate != null)
{
currentCalendar.setTime(startDate);
}
final Calendar calendar = currentCalendar;
DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
//
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.getWindow().clearFlags(Window.FEATURE_ACTION_BAR);
d.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
d.setCustomTitle(linearLayout);
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick (DialogInterface dialog, int which)
{
// do nothing
}
});
//
DatePicker datePicker = d.getDatePicker();
calendar.clear();
calendar.set(2017, Calendar.JANUARY, 1);
datePicker.setMinDate(calendar.getTimeInMillis());
calendar.clear();
calendar.set(2025, Calendar.DECEMBER, 31);
datePicker.setMaxDate(calendar.getTimeInMillis());
//
d.show();
}
void showPickDateDialog (final DatePickerDialog.OnDateSetListener listener, Date startDate)
{
Calendar currentCalendar = Calendar.getInstance();
if (startDate != null)
{
currentCalendar.setTime(startDate);
}
final Calendar calendar = currentCalendar;
DatePickerDialog d = new DatePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
//
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.getWindow().clearFlags(Window.FEATURE_ACTION_BAR);
d.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
d.setCustomTitle(linearLayout);
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick (DialogInterface dialog, int which)
{
MainActivity.log("OBSetupMenu:showPickDateDialog:cancelled!");
loadHomeScreen();
}
});
//
DatePicker datePicker = d.getDatePicker();
calendar.clear();
calendar.set(2017, Calendar.JANUARY, 1);
datePicker.setMinDate(calendar.getTimeInMillis());
calendar.clear();
calendar.set(2025, Calendar.DECEMBER, 31);
datePicker.setMaxDate(calendar.getTimeInMillis());
//
d.show();
}
/**
* {@inheritDoc}
*
* @return A new dialog to let the user choose an expiration date that will be bound to a share link.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Load arguments
mFile = getArguments().getParcelable(ARG_FILE);
// Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
final Calendar chosenDate = Calendar.getInstance();
long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
if (chosenDateInMillis > tomorrowInMillis) {
chosenDate.setTimeInMillis(chosenDateInMillis);
} else {
chosenDate.setTimeInMillis(tomorrowInMillis);
}
// Create a new instance of DatePickerDialog
DatePickerDialog dialog = new DatePickerDialog(
getActivity(),
this,
chosenDate.get(Calendar.YEAR),
chosenDate.get(Calendar.MONTH),
chosenDate.get(Calendar.DAY_OF_MONTH)
);
// Prevent days in the past may be chosen
DatePicker picker = dialog.getDatePicker();
picker.setMinDate(tomorrowInMillis - 1000);
// Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
// in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
// shown by default)
picker.setCalendarViewShown(false);
return dialog;
}
private void datePicker(String targetElement, Calendar initialDate) {
// Remove single-quotes from the `targetElement` CSS selecter, as
// we'll be using these to enclose the entire string in JS. We
// are not trying to properly escape these characters, just prevent
// suprises from JS injection.
final String safeTargetElement = targetElement.replace('\'', '_');
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
++month;
String dateString = String.format(UK, "%04d-%02d-%02d", year, month, day);
String setJs = String.format("$('%s').val('%s').trigger('change')",
safeTargetElement, dateString);
parent.evaluateJavascript(setJs);
}
};
// Make sure that the datepicker uses spinners instead of calendars. Material design
// does not support non-calendar view, so we explicitly use the Holo theme here.
// Rumours suggest this may still show a calendar view on Android 24. This has not been confirmed.
// https://stackoverflow.com/questions/28740657/datepicker-dialog-without-calendar-visualization-in-lollipop-spinner-mode
DatePickerDialog dialog = new DatePickerDialog(parent, android.R.style.Theme_Holo_Dialog, listener,
initialDate.get(YEAR), initialDate.get(MONTH), initialDate.get(DAY_OF_MONTH));
DatePicker picker = dialog.getDatePicker();
picker.setCalendarViewShown(false);
picker.setSpinnersShown(true);
dialog.show();
}
@Override
protected View getInputView() {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
DatePickerDialog datePickerDialog = new DatePickerDialog(
getContext(),
null,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
return datePickerDialog.getDatePicker();
}
static Dialog createDialog(
Bundle args, Context activityContext, @Nullable OnDateSetListener onDateSetListener) {
final Calendar c = Calendar.getInstance();
if (args != null && args.containsKey(DatePickerDialogModule.ARG_DATE)) {
c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_DATE));
}
final int year = c.get(Calendar.YEAR);
final int month = c.get(Calendar.MONTH);
final int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerMode mode = DatePickerMode.DEFAULT;
if (args != null && args.getString(DatePickerDialogModule.ARG_MODE, null) != null) {
mode = DatePickerMode.valueOf(args.getString(DatePickerDialogModule.ARG_MODE).toUpperCase(Locale.US));
}
DatePickerDialog dialog = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
switch (mode) {
case CALENDAR:
dialog = new DismissableDatePickerDialog(activityContext,
activityContext.getResources().getIdentifier("CalendarDatePickerDialog", "style", activityContext.getPackageName()),
onDateSetListener, year, month, day);
break;
case SPINNER:
dialog = new DismissableDatePickerDialog(activityContext,
activityContext.getResources().getIdentifier("SpinnerDatePickerDialog", "style", activityContext.getPackageName()),
onDateSetListener, year, month, day);
break;
case DEFAULT:
dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);
break;
}
} else {
dialog = new DismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day);
switch (mode) {
case CALENDAR:
dialog.getDatePicker().setCalendarViewShown(true);
dialog.getDatePicker().setSpinnersShown(false);
break;
case SPINNER:
dialog.getDatePicker().setCalendarViewShown(false);
break;
}
}
final DatePicker datePicker = dialog.getDatePicker();
if (args != null && args.containsKey(DatePickerDialogModule.ARG_MINDATE)) {
// Set minDate to the beginning of the day. We need this because of clowniness in datepicker
// that causes it to throw an exception if minDate is greater than the internal timestamp
// that it generates from the y/m/d passed in the constructor.
c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MINDATE));
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
datePicker.setMinDate(c.getTimeInMillis());
} else {
// This is to work around a bug in DatePickerDialog where it doesn't display a title showing
// the date under certain conditions.
datePicker.setMinDate(DEFAULT_MIN_DATE);
}
if (args != null && args.containsKey(DatePickerDialogModule.ARG_MAXDATE)) {
// Set maxDate to the end of the day, same reason as for minDate.
c.setTimeInMillis(args.getLong(DatePickerDialogModule.ARG_MAXDATE));
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
datePicker.setMaxDate(c.getTimeInMillis());
}
return dialog;
}