在Android的DatePickerDialog设置限制?
如何限制DatePickerDialogselect过去2 – 12年的date和从当前date0 – 2年的date。 我有DatePickerDialog活动,用于获取婴幼儿的出生date。孩子的年龄将是(2-12岁),婴儿的年龄可以是(0-2ys)。 那么我能做些什么才能在日历中获得这个function。如果我通过1,那么年龄限制将是(2-12岁),我通过2年龄限制将(0-2岁)。请帮助获得我的function压延机。
我的日历代码是:
date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub myCalendar.set(Calendar.YEAR, year); myCalendar.set(Calendar.MONTH, monthOfYear); myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); String myFormat = "dd/MM/yy"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); dateofBirth.setText(sdf.format(myCalendar.getTime())); } }; dateofBirth.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DATE_DIALOG_ID); } }); public Dialog onCreateDialog(int id) { final Calendar now = Calendar.getInstance(); switch (id) { case DATE_DIALOG_ID: // set date picker as current date DatePickerDialog _date = new DatePickerDialog(this, date,myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){ if (year > now.get(Calendar.YEAR)) view.updateDate(myCalendar .get(Calendar.YEAR), myCalendar .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR)) view.updateDate(myCalendar .get(Calendar.YEAR), myCalendar .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && monthOfYear == now.get(Calendar.MONTH)) view.updateDate(myCalendar .get(Calendar.YEAR), myCalendar .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); } }; return _date; } return null; }
所有其他的答案似乎相当复杂,所以我只是想说明一点:你可以从DatePickerDialog
(通过简单的调用getDatePicker()
)得到底层的DatePicker
,并使用下面的方法设置边界:
-
setMinDate(long minDate)
-
setMaxDate(long maxDate)
自variables是默认时区1970年1月1日00:00:00以来的通常毫秒数。 当然,你仍然需要计算这些值,但对于Calendar
类来说,这应该是微不足道的:只需要获取当前date并添加或减去x
年。
更加细心的读者会注意到,前面提到的setter在API级别11之前是不可用的。如果你只是针对那个(或者更新的)平台,那么你就可以走了。 如果你还想支持姜饼设备(Android 2.3 / API级别9+),你可以使用DatePicker
移植版本 。
我有不同的问题。
我使用两个input文本和两个DatePickerDialog,我需要更新在第一个dateselect器对话框中select的动作的第二个,但是因为我在第一个对话框中select,第二个对话框中的minDate不更新。
参考:
Androiddateselect器中的SetMaxdate设置为30天
现在,在下面,启动variables:
private static final int DEPARTUREDATE_DIALOG_ID = 0; private static final int RETURNDATE_DIALOG_ID = 1; private long mMinReturnDate; private int year_x, month_x, day_x;
在下面,第一个datePicker对话框的默认值和minDate:
// onCreate setValues() method. DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault()); Calendar calendar = Calendar.getInstance(); year_x = calendar.get(Calendar.YEAR); month_x = calendar.get(Calendar.MONTH); day_x = calendar.get(Calendar.DAY_OF_MONTH); String departureDatetime = dateFormat.format(calendar.getTime()); mDepartureDate.setText(departureDatetime); // Determine min date to select in calendar. mMinReturnDate = calendar.getTimeInMillis(); calendar.add(Calendar.DATE, 1); String returnDatetime = dateFormat.format(calendar.getTime()); mReturnDate.setText(returnDatetime);
onCreateDialog方法:
protected Dialog onCreateDialog(int id) { super.onCreateDialog(id); if (id == DEPARTUREDATE_DIALOG_ID) return showCalendarFromDepartureDate(mContext, year_x, month_x, day_x); else if (id == RETURNDATE_DIALOG_ID) return showCalendarFromReturnDate(mContext, year_x, month_x, day_x); return null; }
下面showCalendarFromDepartureDate
方法:
在这里,我做了代码来更新第二个datepicker对话框的minDate,我select了第一个对话框。
/** * Show calendar from departure date. * * @param context * @param year * @param month * @param day * @return */ protected DatePickerDialog showCalendarFromDepartureDate(Context context, int year, int month, int day) { Calendar now = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(context, departureDatePickerLstner, year, month, day); dialog.getDatePicker().setMinDate(now.getTimeInMillis()); return dialog; } private DatePickerDialog.OnDateSetListener departureDatePickerLstner = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault()); year_x = year; month_x = month; day_x = dayOfMonth; Calendar newDate = Calendar.getInstance(); newDate.set(year_x, month_x, day_x); // Update minDate to date of return. mMinReturnDate = newDate.getTimeInMillis(); String departureDatetime = dateFormat.format(newDate.getTime()); mDepartureDate.setText(departureDatetime); newDate.add(Calendar.DATE, 1); String returnDatetime = dateFormat.format(newDate.getTime()); mReturnDate.setText(returnDatetime); } };
问题是,我第二次更新第一个日历的date,第二次的minDate
总是第一次select它的旧date。
/** * Show calendar from return date. * * @param context * @param year * @param month * @param day * @return */ protected DatePickerDialog showCalendarFromReturnDate(Context context, int year, int month, int day) { DatePickerDialog dialog = new DatePickerDialog(context, returnDatePickerLstner, year, month, day); dialog.getDatePicker().setMinDate(mMinReturnDate); return dialog; } private DatePickerDialog.OnDateSetListener returnDatePickerLstner = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault()); year_x = year; month_x = month; day_x = dayOfMonth; Calendar newDate = Calendar.getInstance(); newDate.set(year_x, month_x, day_x); String returnDatetime = dateFormat.format(newDate.getTime()); mReturnDate.setText(returnDatetime); } };
在第一个对话框中selectdate
在第二个对话框中selectdate
在第一个对话框中更新date
在这里,第二个对话框中的错误
。
*编辑:我怀疑DatePickerDialog没有创build每次我点击input,所以我决定debugging第二个dateselect器对话框showCalendarFromReturnDate ()
,它并不总是发生在这里。
在第二个datepicker对话框的监听器中,我在最后一个方法onDataSet ()
了一个删除对话框removeDialog (RETURNDATE_DIALOG_ID);
但即使如此,有时minDate没有设置,因为在启动对话框之前,也不会通过该方法。
我不知道该怎么办。
@Override protected Dialog onCreateDialog(int id) { super.onCreateDialog(id); if (id == DEPARTUREDATE_DIALOG_ID) return showCalendarFromDepartureDate(mContext, year_x, month_x, day_x); else if (id == RETURNDATE_DIALOG_ID) return showCalendarFromReturnDate(mContext, year_x, month_x, day_x); return null; } ... protected DatePickerDialog showCalendarFromReturnDate(Context context, int year, int month, int day) { Log.d(TAG, "==== DatePickerDialog from returnDate ===="); Log.d(TAG, "minDate is: " + mMinReturnDate); Log.d(TAG, "year is: " + year); Log.d(TAG, "month is: " + month); Log.d(TAG, "day is: " + day); DatePickerDialog dialog = new DatePickerDialog(context, returnDatePickerLstner, year, month, day); dialog.getDatePicker().setMinDate(mMinReturnDate); return dialog; } private DatePickerDialog.OnDateSetListener returnDatePickerLstner = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM yyyy", Locale.getDefault()); year_x = year; month_x = month; day_x = dayOfMonth; Calendar newDate = Calendar.getInstance(); newDate.set(year_x, month_x, day_x); String returnDatetime = dateFormat.format(newDate.getTime()); mReturnDate.setText(returnDatetime); //@TODO: I try to resolve this bug with this but it doesn't always work. removeDialog(RETURNDATE_DIALOG_ID); } };
发生什么事了?
由于我正在巡视并且无法访问我的电脑,因此我无法给您提供工作代码。 但我可以给你一个方法来实现你的目标(或者我认为)。
我不确定这会完美的工作,但任何方式值得一试。
请尝试此代码并通知我。
“DatepickerType”是类的一个Integer公共成员variables。
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { int y = Calendar.getInstance().get(Calendar.YEAR); switch(DatepickerType){ case 1: if(selectedYear>(y-2)){ //Add message if you want selectedYear = y-2; } else if(selectedYear<(y-12)) { //Add msg if u want selectedYear = y-12; } case 2: if(selectedYear>(y)){ //Add message if you want selectedYear = y; } else if(selectedYear<(y-2)) { //Add msg if u want selectedYear = y-2; } } String year1 = String.valueOf(selectedYear); String month1 = String.valueOf(selectedMonth+1); String day1 = String.valueOf(selectedDay); //do what you need setLastStatus(); } };
这可以帮助你。更好的使用对话框片段,因为Dailog的showDialog()被删除。 此外,请记住,它只能在高于11.so的API中工作,请将清单min.sdk更改为11
Calendar calendar=Calendar.getInstance(); int mDay = calendar.get(Calendar.DAY_OF_MONTH), mMonth = calendar.get(Calendar.MONTH), mYear = calendar.get(Calendar.YEAR); int minDay = calendar.get(Calendar.DAY_OF_MONTH), minMonth = calendar.get(Calendar.MONTH), minYear = calendar.get(Calendar.YEAR)-12; int maxDay = calendar.get(Calendar.DAY_OF_MONTH), maxMonth = calendar.get(Calendar.MONTH), maxYear = calendar.get(Calendar.YEAR)-12; public void onDate(View view) { System.out.println("hello"); DialogFragment fragment = new SelectDateFragment(); fragment.show(getFragmentManager(), "Date Picker"); } class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public Dialog onCreateDialog(Bundle savedInstanceState) { System.out.println("entrering on create dialog");; return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay) { public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { System.out.println("----------onDateChanged()-----------" + year + "--" + year); System.out.println("----------onDateChanged()-----------" + monthOfYear + "--" + monthOfYear); System.out.println("----------onDateChanged()-----------" + dayOfMonth + "--" + dayOfMonth); if ((minYear < year) || ((minMonth < monthOfYear) && (minYear == year)) || ((minDay < dayOfMonth) && (minYear == year) && (minMonth == monthOfYear))) { view.updateDate(minYear, minMonth - 1, minDay); } if ((maxYear > year) || ((maxMonth > monthOfYear) && (maxYear == year)) || ((maxDay > dayOfMonth) && (maxYear == year) && (maxMonth == monthOfYear))) { view.updateDate(maxYear, maxMonth - 1, maxDay); } } }; } @Override public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { System.out.println("year=" + year + "day=" + dayOfMonth + "month=" + monthOfYear); mYear=year; mMonth=monthOfYear; mDay=dayOfMonth; onPopulateSet(year, monthOfYear + 1, dayOfMonth); } private void onPopulateSet(int year, int i, int dayOfMonth) { EditText et_setDate; et_setDate = (EditText) findViewById(R.id.register_et_dob); et_setDate.setText(dayOfMonth + "/" + i + "/" + year); System.out.println("enetring on populate Set"); } }
datePickerDialog = new CustomDatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub try { Log.i("DAta SEt", "data set"); } catch (Exception e) { } } }, minYear, minMonth, minDay, maxYear, maxMonth, maxDay);
CustomDatePickerDialog:
public class CustomDatePickerDialog extends DatePickerDialog { private boolean mIgnoreEvent = false, mignorsunday = true; public static int mYear, mMonth, mDate, maxYear, maxMonth, maxDay, minYear, minMonth, minDay; public static int dateflag = 0; public static int dateflag2 = 0; public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth, int maxYear, int maxMonth, int maxDay) { super(context, callBack, year, monthOfYear, dayOfMonth); // TODO Auto-generated constructor stub dateflag = 0; dateflag2 = 1; mYear = year; mMonth = monthOfYear; mDate = dayOfMonth; Log.i("Hello World ::", "Please Its Updating At Every time"); this.maxYear = maxYear; this.maxMonth = maxMonth; this.maxDay = maxDay; this.minYear = year; this.minMonth = monthOfYear; this.minDay = dayOfMonth; // setTitle("בחרו מתי נח לכם שנתקשר"); setTitle("We Call U...בחרו מתי נוח לכם ש"); } @Override public void onDateChanged(DatePicker view, int year, int month, int day) { // TODO Auto-generated method stub super.onDateChanged(view, year, month, day); dateflag = 1; dateflag2 = 1; if (!mIgnoreEvent) { mIgnoreEvent = true; if (year > maxYear || month > maxMonth && year == maxYear || day > maxDay && year == maxYear && month == maxMonth) { mYear = maxYear; mMonth = maxMonth; mDate = maxDay; view.updateDate(maxYear, maxMonth, maxDay); } else if (year < minYear || month < minMonth && year == minYear || day < minDay && year == minYear && month == minMonth) { mYear = minYear; mMonth = minMonth; mDate = minDay; view.updateDate(minYear, minMonth, minDay); } else { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DATE, day); if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { if (mignorsunday) { cal.add(Calendar.DATE, 1); mYear = cal.get(Calendar.YEAR); mMonth = cal.get(Calendar.MONTH); mDate = cal.get(Calendar.DATE); mignorsunday = false; } else if (!mignorsunday) { mignorsunday = true; cal.add(Calendar.DATE, -1); mYear = cal.get(Calendar.YEAR); mMonth = cal.get(Calendar.MONTH); mDate = cal.get(Calendar.DATE); } } else { mYear = year; mMonth = month; mDate = day; } view.updateDate(mYear, mMonth, mDate); } setTitle("We Call U...בחרו מתי נוח לכם ש"); mIgnoreEvent = false; } } public int getSelectedYear() { return mYear; } public int getSelectedMonth() { return mMonth; } public int getSelectedDate() { return mDate; } }
这里是工作代码,你必须根据你的代码做一些改变。 就像你可以添加儿童和婴儿的最小和最大年龄。
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { /* * mYear = year; * mMonth = monthOfYear; * mDay = dayOfMonth; */ if (year > maxYear || monthOfYear > maxMonth && year == maxYear || dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth) { view.updateDate(maxYear, maxMonth, maxDay); //you can add toast message here for max year selection } else if (year < minYear || monthOfYear < minMonth && year == minYear || dayOfMonth < minDay && year == minYear && monthOfYear == minMonth) { view.updateDate(minYear, minMonth, minDay); //you can add toast message here for min year selection } else { view.updateDate(year, monthOfYear, dayOfMonth); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPickDate = (Button) findViewById(R.id.pickDate); mPickDate.setOnClickListener(this); final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); maxYear = mYear - 2;//make a proper condition to change it the based on the child/infant maxMonth = mMonth; maxDay = mDay; minYear = mYear - 12;//make a proper condition to change it the based on the child/infant minMonth = mMonth; minDay = mDay; } @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear - minYear, mMonth, mDay); } return null; }
如果您不想使用自定义对话框,请使用这一行代码
。datePickerDialog.getDatePicker()setMinDate(System.currentTimeMillis的());
设置DatePickerDialog的最小和最大date是不够的。 用户可以点击年份并在年份之间滚动。
您还应该像这样在DatePicker上设置年份范围:
datePicker.setYearRange(calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR) + 1);