设置minDate和maxDate

时间:2015-12-18 07:21:09

标签: android android-fragments datepicker

我正在使用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(),
                AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

        /*
            add(int field, int value)
                Adds the given amount to a Calendar field.
         */
        // Add 3 days to Calendar
        calendar.add(Calendar.DATE, 3);

        /*
            getTimeInMillis()
                Returns the time represented by this Calendar,
                recomputing the time from its fields if necessary.

            getDatePicker()
            Gets the DatePicker contained in this dialog.

            setMinDate(long minDate)
                Sets the minimal date supported by this NumberPicker
                in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

            setMaxDate(long maxDate)
                Sets the maximal date supported by this DatePicker in milliseconds
                since January 1, 1970 00:00:00 in getDefault() time zone.
         */

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

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

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

        // So, now date picker selectable date range is 7 days only

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

    @Override
    public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // 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, monthOfYear, dayOfMonth, 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);
    }
}

我在点击视图中调用此类。

1 个答案:

答案 0 :(得分:0)

从DatePickerFragment中删除重复的方法onDateSet(DatePicker, int, int, int),如下所示

@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    // 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, monthOfYear, dayOfMonth, 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);
}