下面列出了android.app.DatePickerDialog#show ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showNativeCalendar() {
Calendar calendar = DateUtils.getInstance().getCalendar();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, year, month, dayOfMonth) -> {
Calendar chosenDate = Calendar.getInstance();
chosenDate.set(year, month, dayOfMonth);
presenter.rescheduleEvent(chosenDate.getTime());
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
datePickerDialog.setButton(DialogInterface.BUTTON_NEUTRAL, getContext().getResources().getString(R.string.change_calendar), (dialog, which) -> {
datePickerDialog.dismiss();
showCustomCalendar();
});
}
datePickerDialog.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_reminder_edit) {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(AudioNoteActivity.this, this, year, month, day);
dpd.getDatePicker().setMinDate(new Date().getTime());
dpd.show();
return true;
} else if (id == R.id.action_reminder_delete) {
cancelNotification();
return true;
}
return false;
}
private void showDatePicker(String msg, final EditText targetInput) {
final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (targetInput != null) {
targetInput.setText(String.format(Locale.US, "%02d-%02d-%02d", year, 1 + monthOfYear, dayOfMonth));
}
}
}, mYear, mMonth, mDay);
datePickerDialog.setMessage(msg);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
@Override
public void onClick(View v) {
mCurrentDate = Calendar.getInstance();
int mYear = mCurrentDate.get(Calendar.YEAR);
int mMonth = mCurrentDate.get(Calendar.MONTH);
int mDay = mCurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(BasicActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
// Update the editText to display the selected date
mDateEditText.setText(selectedDay + "-" + (selectedMonth + 1) + "-" + selectedYear);
// Set the mCurrentDate to the selected date-month-year
mCurrentDate.set(selectedYear, selectedMonth, selectedDay);
mGeneratedDateIcon = mImageGenerator.generateDateImage(mCurrentDate, R.drawable.empty_calendar);
mDisplayGeneratedImage.setImageBitmap(mGeneratedDateIcon);
}
}, mYear, mMonth, mDay);
mDatePicker.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_reminder_edit) {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(ChecklistNoteActivity.this, this, year, month, day);
dpd.getDatePicker().setMinDate(new Date().getTime());
dpd.show();
return true;
} else if (id == R.id.action_reminder_delete) {
cancelNotification();
return true;
}
return false;
}
@OnClick(R.id.showDatePicker)
void onShowDatePickerClick() {
Calendar now = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(
SampleActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
},
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
private void showDatePickerDialog(String initialValue) {
String[] dateArray = initialValue.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(dateArray[0]), Integer.parseInt(dateArray[1]) - 1, Integer.parseInt(dateArray[2]));
DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Update the date
mShow.setFirstAiredDate(year, monthOfYear + 1, dayOfMonth);
// Update the UI with the new value
setupValues(false);
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_reminder_edit) {
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(TextNoteActivity.this, this, year, month, day);
dpd.getDatePicker().setMinDate(new Date().getTime());
dpd.show();
return true;
} else if (id == R.id.action_reminder_delete) {
cancelNotification();
return true;
}
return false;
}
@Override
public void onClick(View v) {
mCurrentDate = Calendar.getInstance();
int mYear = mCurrentDate.get(Calendar.YEAR);
int mMonth = mCurrentDate.get(Calendar.MONTH);
int mDay = mCurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(TypeFaceActivity.this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
// Update the editText to display the selected date
mDateEditText.setText(selectedDay + "-" + (selectedMonth + 1) + "-" + selectedYear);
// Set the mCurrentDate to the selected date-month-year
mCurrentDate.set(selectedYear, selectedMonth, selectedDay);
mGeneratedDateIcon = mImageGenerator.generateDateImage(mCurrentDate, R.drawable.empty_calendar);
mDisplayGeneratedImage.setImageBitmap(mGeneratedDateIcon);
}
}, mYear, mMonth, mDay);
mDatePicker.show();
}
private void pickDate(TextView tv) {
Object tag = tv.getTag();
final Calendar cal = (tag == null ? Calendar.getInstance() : (Calendar) tag);
DatePickerDialog picker = new DatePickerDialog(getContext(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
DateFormat DF = Helper.getDateInstance(getContext());
tv.setTag(cal);
tv.setText(DF.format(cal.getTime()));
}
},
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
picker.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
tv.setTag(null);
tv.setText(null);
}
});
picker.show();
}
@OnClick(R.id.tg_time)
public void getTime() {
Calendar calender = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(DateWriteActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calender.set(year, monthOfYear, dayOfMonth);
tgTime.setText(new SimpleDateFormat("yyyy年MM月dd日").format(calender.getTime()));
time = calender.getTime().getTime();
}
}, calender.get(Calendar.YEAR), calender.get(Calendar.MONTH), calender.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
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();
}
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();
}
private void showDatePicker(final EditText birthday) {
DatePickerDialog dpd =
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Date selectedDate = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();
birthday.setText(sdf.format(selectedDate));
}
}, 2016, 0, 1);
dpd.show();
}
@RequiresApi(api = VERSION_CODES.O)
private void showDatePicker(LocalDate hint, int titleResId, DatePickResult resultCallback) {
DatePickerDialog picker = new DatePickerDialog(getActivity(),
(pickerObj, year, month, day) -> {
final LocalDate pickedDate = LocalDate.of(year, month + 1, day);
resultCallback.onResult(pickedDate);
}, hint.getYear(), hint.getMonth().getValue() - 1, hint.getDayOfMonth());
picker.setTitle(getString(titleResId));
picker.show();
}
private void openDate() {
datePicker = new DatePickerDialog(this, mDateSetListenerSatrt,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePicker.show();
}
public void showDatePicker() {
DatePickerDialog cc = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, dateSetListener, 1990,
1, 1);
cc.show();
}
private void showNativeCalendar(String programUid, String uid, OrgUnitDialog orgUnitDialog) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dateDialog = new DatePickerDialog(view.getContext(), (
(datePicker, year1, month1, day1) -> {
Calendar selectedCalendar = Calendar.getInstance();
selectedCalendar.set(Calendar.YEAR, year1);
selectedCalendar.set(Calendar.MONTH, month1);
selectedCalendar.set(Calendar.DAY_OF_MONTH, day1);
selectedCalendar.set(Calendar.HOUR_OF_DAY, 0);
selectedCalendar.set(Calendar.MINUTE, 0);
selectedCalendar.set(Calendar.SECOND, 0);
selectedCalendar.set(Calendar.MILLISECOND, 0);
selectedEnrollmentDate = selectedCalendar.getTime();
compositeDisposable.add(getOrgUnits(programUid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
allOrgUnits -> {
ArrayList<OrganisationUnit> orgUnits = new ArrayList<>();
for (OrganisationUnit orgUnit : allOrgUnits) {
boolean afterOpening = false;
boolean beforeClosing = false;
if (orgUnit.openingDate() == null || !selectedEnrollmentDate.before(orgUnit.openingDate()))
afterOpening = true;
if (orgUnit.closedDate() == null || !selectedEnrollmentDate.after(orgUnit.closedDate()))
beforeClosing = true;
if (afterOpening && beforeClosing)
orgUnits.add(orgUnit);
}
if (orgUnits.size() > 1) {
orgUnitDialog.setOrgUnits(orgUnits);
if (!orgUnitDialog.isAdded())
orgUnitDialog.show(view.getAbstracContext().getSupportFragmentManager(), "OrgUnitEnrollment");
} else
enrollInOrgUnit(orgUnits.get(0).uid(), programUid, uid, selectedEnrollmentDate);
},
Timber::d
)
);
}),
year,
month,
day);
Program selectedProgram = getProgramFromUid(programUid);
if (selectedProgram != null && !selectedProgram.selectEnrollmentDatesInFuture()) {
dateDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
}
if (selectedProgram != null) {
dateDialog.setTitle(selectedProgram.enrollmentDateLabel());
}
dateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, view.getContext().getString(R.string.date_dialog_clear), (dialog, which) -> {
dialog.dismiss();
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
dateDialog.setButton(DialogInterface.BUTTON_NEUTRAL, view.getContext().getResources().getString(R.string.change_calendar), (dialog, which) -> {
dateDialog.dismiss();
showCustomCalendar(programUid, uid, orgUnitDialog);
});
}
dateDialog.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
setShareIntent();
//noinspection SimplifiableIfStatement
if (id == R.id.action_reminder) {
//open the schedule dialog
final Calendar c = Calendar.getInstance();
//fill the notificationCursor
notificationCursor = DbAccess.getNotificationByNoteId(getBaseContext(), this.id);
hasAlarm = notificationCursor.moveToFirst();
if (hasAlarm) {
notification_id = notificationCursor.getInt(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_ID));
}
if (hasAlarm) {
//ask whether to delete or update the current alarm
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.action_reminder));
popupMenu.inflate(R.menu.reminder);
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
} else {
//create a new one
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(AudioNoteActivity.this, this, year, month, day);
dpd.getDatePicker().setMinDate(c.getTimeInMillis());
dpd.show();
}
return true;
} else if (id == R.id.action_save) {
if (ContextCompat.checkSelfPermission(AudioNoteActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(AudioNoteActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityCompat.requestPermissions(AudioNoteActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_EXTERNAL_STORAGE);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(AudioNoteActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_EXTERNAL_STORAGE);
}
} else {
saveToExternalStorage();
}
return true;
}
return super.onOptionsItemSelected(item);
}
protected void editDialog(Context context) {
if (!mActivateDialog) return;
if (mCustomerDialogBuilder != null) {
mCustomerDialogBuilder.customerDialogBuilder(this).show();
} else {
if ((valueTextView.getInputType() & InputType.TYPE_CLASS_DATETIME) > 0) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(DateConverter.localDateStrToDate(getText(), getContext()));
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(
getContext(), this, year, month, day);
datePickerDialog.show();
} else {
final EditText editText = new EditText(context);
editText.setText(mTextValue);
editText.setGravity(Gravity.CENTER);
editText.setInputType(textViewInputType);
editText.requestFocus();
LinearLayout linearLayout = new LinearLayout(context.getApplicationContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(editText);
final SweetAlertDialog dialog = new SweetAlertDialog(context, SweetAlertDialog.NORMAL_TYPE)
.setTitleText(mTitle)
.setCancelText(getContext().getString(R.string.global_cancel))
.setHideKeyBoardOnDismiss(true)
.setCancelClickListener(sDialog -> {
editText.clearFocus();
Keyboard.hide(context, editText);
sDialog.dismissWithAnimation();})
.setConfirmClickListener(sDialog -> {
editText.clearFocus();
Keyboard.hide(context, editText);
setText(editText.getText().toString());
sDialog.dismissWithAnimation();
if (mConfirmClickListener != null)
mConfirmClickListener.onTextChanged(EditableInputView.this);
});
dialog.setOnDismissListener(sDialog -> {
rootView.requestFocus();
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
if ( imm !=null)
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);});
//Keyboard.hide(context, editText);});
dialog.setOnShowListener(sDialog -> {
editText.requestFocus();
Keyboard.show(context, editText);
});
dialog.setCustomView(linearLayout);
dialog.show();
}
}
}