限制Android datepicker对话框不起作用?如何解决这个问题?

时间:2017-02-08 06:35:52

标签: android

我想在上个月才显示日期。而且到今天为止的日期不是未来的日期。

          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double quote
          \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
          \uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)
          \UHHHHHHHH
                 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)
          \cx    a control-x character

2 个答案:

答案 0 :(得分:1)

试试这个,

    fromDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        final Calendar newCalendar = Calendar.getInstance();
        datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
          public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            newCalendar.set(year, monthOfYear, dayOfMonth);

            /*Set max date*/
            datePickerDialog.getDatePicker().getDatePicker().setMaxDate(new Date().getTime());

            // Subtract 1 month from Calendar updated date
             c.add(Calendar.MONTH, -1);

             // Set the Calendar new date as minimum date of date picker
              datePickerDialog.getDatePicker() .getDatePicker().setMinDate(newCalendar.getTimeInMillis());

          }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
        }
});

答案 1 :(得分:0)

您可以使用DatePicker的maxDate和minDate属性,并且可以使用Java Code完成相同的操作。您可以参考link获取更多信息