我试图在DatePickerDialog中禁用特定日期的日期

时间:2018-01-28 09:15:00

标签: android calendar datepickerdialog

我试图在android中的DatePickerDialog中禁用特定日期的日期,我知道如何设置最小和最大限制,但有没有办法在同一个时间禁用一个月的特定日期

代码:

  public static class DatePickerFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final 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 dpd = new DatePickerDialog(getActivity(), this, year, month, day);

        calendar.add(Calendar.MONTH, 3);

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

        // Subtract 6 days from Calendar updated date
        calendar.add(Calendar.MONTH, -3);

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




        // Return the DatePickerDialog
        return  dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        // Do something with the chosen date
        TextView tv = (TextView) getActivity().findViewById(R.id.tv);

        // Create a Date variable/object with user chosen date
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(year, month, day, 0, 0, 0);
        Date chosenDate = cal.getTime();

        // Format the date using style and locale
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
        String formattedDate = df.format(chosenDate);

        // Display the chosen date to app interface
        tv.setText(formattedDate);
    }
}

0 个答案:

没有答案