Android DatePickerDialog:设置选择的最小和最大日期

Android DatePickerDialog: Set min and max date for selection

我知道对此有很多问题,但 none 的解决方案对我有用,所以这个问题。我想将用户限制在今天之前的 select 日期,但我做不到。

public class DatePickerDialogFragment extends DialogFragment {

    private OnDateSetListener listener;

    public void setListener(OnDateSetListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year    = calendar.get(Calendar.YEAR);
        int month   = calendar.get(Calendar.MONTH);
        int day     = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
        dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
        return dialog;
    }
}

我显示为:

DatePickerDialogFragment fragment = new DatePickerDialogFragment();
fragment.setListener(dateSetListener);
fragment.show(getSupportFragmentManager(), "Choose booking date");

我希望用户不能 select 在今天之前约会。如您所见,我用今天的时间调用了 setMinDate() 方法,但没有效果。该对话框将今天之前的日期显示为灰色但 select 可用。

我还尝试按照一些 Whosebug 答案中的建议子 class DatePickerDialog 并覆盖 onDateChanged 但没有任何成功。

Above API 11 对于最短日期,您可以使用

android:minDate="mm/dd/yyyy"setMinDate(long time) 对于最大日期

android:maxDate="mm/dd/yyyy" // 在 XML

setMaxDate(new Date().getTime());//当前时间

获取系统时间并从中减去 1 秒

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year    = calendar.get(Calendar.YEAR);
    int month   = calendar.get(Calendar.MONTH);
    int day     = calendar.get(Calendar.DAY_OF_MONTH);

     dialog = new DatePickerDialog(act, listener, year, month, day);
     dialog.getDatePicker().setMinDate(System.currentTimeMillis() -1000);
     return dialog;
 }

试试这个方法

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year    = calendar.get(Calendar.YEAR);
    int month   = calendar.get(Calendar.MONTH);
    int day     = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
    Field mDatePickerField;
    try {
            mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
            mDatePickerField.setAccessible(true);
    } catch (Exception e) {
            e.printStackTrace();
    }
    dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    return dialog;
}

而不是你的

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year    = calendar.get(Calendar.YEAR);
    int month   = calendar.get(Calendar.MONTH);
    int day     = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
    dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
    return dialog;
}

编辑 1:

我也遇到了这个问题,即用户可以 select not-selectable date in Android L 5.0.2。目前有错误报告 here。在Android L 5.1.0.

中解决

对于此问题的临时解决方案,您可以将 selected 日期与当前系统日期进行比较,并在此基础上设置一些条件。我用这个作为解决方法

EDIT2:

DatePickerDialogFragment中添加onDateSent()方法,只检查它是否早于您在setMinDate()中设置的日期。如果是,则再次显示 DatePickerDialog

final long today = System.currentTimeMillis() - 1000;

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, monthOfYear, dayOfMonth);
            //If user tries to select date in past (or today)
            if (calendar.getTimeInMillis() < today)
            {
                //Make them try again
               DatePickerDialogFragment fragment = new DatePickerDialogFragment();
               fragment.setListener(dateSetListener);
               fragment.show(getSupportFragmentManager(), "Choose booking date");
               Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
            }
            else
            {
                //success
            }
}
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());

我使用该方法将最小日期设置为 2020-05-20 当前年份

datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());

当您点击日期选择器时,它会显示 2020 年作为最小年份

希望回答对您有所帮助。

在XML文件中

<DatePicker
android:minDate="mm/dd/yyyy"
android:maxDate="mm/dd/yyyy" >

例如:

android:minDate="10/12/2000"


android:maxDate="10/12/2000"
  mdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar calendar = Calendar.getInstance();
            //set time zone
            calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

            int selectedYear = calendar.get(Calendar.YEAR);
            int selectedMonth = calendar.get(Calendar.MONTH);
            int selectedDay = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(BrandResturant_DetailActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker view, int selectedYear,
                                              int selectedMonth, int selectedDay) {
                            mDay = selectedDay;
                            mMonth = selectedMonth;
                            mYear = selectedYear;
                            StringBuilder Date = new StringBuilder("");
                            String conver = Integer.toString(selectedYear);
                            Date.append(conver);
                            Date.append("-");
                            selectedMonth++;
                            conver = Integer.toString(selectedMonth);
                            Date.append(conver);
                            Date.append("-");
                            conver = Integer.toString(selectedDay);
                            Date.append(conver);
                            isDob = true;
                        }
                    }, mDay, mMonth, mYear);

            datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());


            
            //Getting Today date to calendar2
            final Calendar calendar2 = Calendar.getInstance();
            //Set Minimum date of calendar
            calendar2.set(2021, 1, 3);
            datePickerDialog.getDatePicker().setMinDate(calendar2.getTimeInMillis());
            datePickerDialog.setTitle("Select Date");
            datePickerDialog.show();


            //Getting Today date to calendar3
            final Calendar calendar3 = Calendar.getInstance();
            //Set Maximum date of calendar - Method 1
            calendar3.set(2021, 3, 3);
            //Set Maximum One Month date from today(calendar3) date to calendar -Method 2
            //In Place of month u can also set year, day
            //calendar3.add(Calendar.MONTH, 1);
            datePickerDialog.getDatePicker().setMaxDate(calendar3.getTimeInMillis());
            datePickerDialog.setTitle("Select Date");
            datePickerDialog.show();
        }
    });