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

时间:2015-10-10 06:57:05

标签: android android-datepicker

我知道这有很多问题,但没有一个解决方案适合我,所以这个问题。我想限制用户在今天之前选择日期,但我无法这样做。

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");

我希望用户不应该在今天之前选择日期。正如你所看到的,我今天用setMinDate()方法调用了它,但它没有效果。该对话框显示今天之前的日期为灰色但可选择。

我还试图按照某些stackoverflow答案中的建议对子类DatePickerDialog进行覆盖并覆盖onDateChanged,但没有任何成功。

7 个答案:

答案 0 :(得分:15)

试试这个方法

@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;
}

<强> EDIT1:

我也遇到过这个问题,用户可以在Android L 5.0.2中选择不可选择的日期。目前有错误报告here。它在Android L 5.1.0中解决。

对于此问题的临时解决方案,您可以将所选日期与当前系统日期进行比较,并根据该日期添加一些条件。我用这个作为我的解决方法

<强> EDIT2:

onDateSent()中添加DatePickerDialogFragment方法,然后检查它是否早于您在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
            }
}

答案 1 :(得分:5)

获取系统时间并从中减去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;
 }

答案 2 :(得分:3)

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

答案 3 :(得分:1)

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

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

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

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

答案 4 :(得分:1)

我使用这样的方法将最短日期设置为当前年份,例如2020-05-20

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

单击日期选择器的那一刻,它将显示2020作为最小年份

我希望答案会有所帮助。

答案 5 :(得分:0)

在XML文件中

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

例如:

android:minDate="10/12/2000"


android:maxDate="10/12/2000"

答案 6 :(得分:-1)

  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();
        }
    });