Android中如何设置DatePicker和TimePicker的限制

How to set restrictions for DatePicker and TimePicker in Android

我想在日期选择器上设置限制,以便它只允许用户选择从现在起 7 天后的日期。我还想在 Timepicker 上设置限制,以便用户只能选择 11:00 am 和 6:00 pm 之间的时间。这是我的 DatePicker 和 TimePicker 代码。

    private void showTimeDialog(final EditText timeInput) {

        final Calendar calendar = Calendar.getInstance();

        TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);

                timeInput.setText(enteredTime.format(calendar.getTime()));
                time = enteredTime.format(calendar.getTime());

            }
        };

        new TimePickerDialog(OrderPickupActivity.this, timeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show();

    }

    private void showDateDialog(final EditText dateInput) {

        final Calendar calendar = Calendar.getInstance();

        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                calendar.set(Calendar.YEAR,year);
                calendar.set(Calendar.MONTH,month);
                calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

                dateInput.setText(enteredDate.format(calendar.getTime()));
                date = enteredDate.format(calendar.getTime());
            }
        };

        new DatePickerDialog(OrderPickupActivity.this, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

    }

你能帮帮我吗?

    private void showDateDialog(final EditText dateInput) {

            final Calendar calendar = Calendar.getInstance();

            DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                    calendar.set(Calendar.YEAR,year);
                    calendar.set(Calendar.MONTH,month);
                    calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

                    dateInput.setText(enteredDate.format(calendar.getTime()));
                    date = enteredDate.format(calendar.getTime());
                }
            };

          DatePickerDialog  datePickerDialog =   new 
   DatePickerDialog(OrderPickupActivity.this, dateSetListener, 
 calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 
 calendar.get(Calendar.DAY_OF_MONTH));
 Calendar c = Calendar.getInstance();
  c.setTime(new Date()); // Now use today date.
  c.add(Calendar.DATE, 7); // Adding 7 days
 datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
 datePickerDialog.show();
        }