如何禁用Androiddateselect器中的过去date?

如何禁用Androiddateselect器中的过去date?

@Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: // set date picker as current date return new DatePickerDialog(this, datePickerListener, year, month, day); } return null; } private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth+1; day = selectedDay; startdate.setText(new StringBuilder().append(day).append("-") .append(getMonth(month + 1)).append("-").append(year) .append(" ")); } }; 

Solutions Collecting From Web of "如何禁用Androiddateselect器中的过去date?"

你可以做

 datePicker.setMinDate(System.currentTimeMillis() - 1000); 

将今天的date设置为最短date,并禁止所有过去的date。

datePickerDatePicker一个对象,如果您使用DatePickerDialog的对象,您可以执行

 datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 

注意: setMinDate是在API 11中引入的

此方法将正常工作。

 //Get yesterday's date Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); //Set yesterday time milliseconds as date pickers minimum date DatePickerDialog datePickerDialog = new DatePickerDialog(context, myDateListener, year, month, day); datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis()); datePickerDialog.show(); 

这是我如何做到的:

 public class DatePickerFragment extends DialogFragment { OnDateSetListener ondateSet; Calendar c; int year = 0, month = 0, day = 0; public DatePickerFragment() { } public void setCallBack(OnDateSetListener ondate) { ondateSet = ondate; } public static DatePickerFragment newInstance(Bundle bundle) { DatePickerFragment myFragment = new DatePickerFragment(); myFragment.setArguments(bundle); return myFragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // final Calendar c = Calendar.getInstance(); //if else for null arguments if (getArguments() != null) { year = getArguments().getInt("year"); month = getArguments().getInt("month"); day = getArguments().getInt("day"); c = Calendar.getInstance(); c.set(year, month, day); } else { c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); Log.d("else", "else"); } DatePickerDialog picker = new DatePickerDialog(getActivity(), ondateSet, year, month, day); picker.getDatePicker().setMinDate(c.getTime().getTime()); Log.d("picker timestamp", c.getTime().getTime() + ""); return picker; } } 

这是你如何实例化select器:

 Bundle bundle = new Bundle(); Calendar c = Calendar.getInstance(); bundle.putInt("year", c.get(Calendar.YEAR)); bundle.putInt("month", c.get(Calendar.MONTH)); bundle.putInt("day", c.get(Calendar.DAY_OF_MONTH)); DatePickerFragment fragment = DatePickerFragment .newInstance(bundle); fragment.setCallBack(dateSet); fragment.show(getActivity().getSupportFragmentManager(), null); 

这是我的实现随时更改它。 请注意,此代码默认设置为currnetdate。 新实例也很重要。

你可以使用这行代码到你的代码…

 private Calendar cal; private int day; private int month; private int year; static final int DATE_PICKER_ID = 1111; TextView textView; textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cal = Calendar.getInstance(); day = cal.get(Calendar.DAY_OF_MONTH); month = cal.get(Calendar.MONTH); year = cal.get(Calendar.YEAR); showDialog(DATE_PICKER_ID); } }); @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: // create a new DatePickerDialog with values you want to show DatePickerDialog datePickerDialog = new DatePickerDialog(this, datePickerListener, year, month, day); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, 0); // Add 0 days to Calendar Date newDate = calendar.getTime(); datePickerDialog.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000))); return datePickerDialog; } return null; } private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // the callback received when the user "sets" the Date in the // DatePickerDialog public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { textViewTime.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear); } }; 

好的,所以我使用MaterialDatePicker库,它太简单了,以设置它的最低date。

 DatePickerDialog mDatePickerDialog = DatePickerDialog.newInstance(listener, mTime.year, mTime.month, mTime.monthDay); mDatePickerDialog.setYearRange(2016, 2036); Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis() - 1000); mDatePickerDialog.setMinDate(c);