Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

Android DatePickerDialog: Set min and max date for selection

I know there are quite a lot of question for this but none of the solutions are working for me, so this question. I want to restrict user to select date before today, but am not able to do so.

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

I am showing it as:

DatePickerDialogFragment fragment = new DatePickerDialogFragment();
fragment.setListener(dateSetListener);
fragment.show(getSupportFragmentManager(), "Choose booking date");

I want the user should not be able to select date before today. As you can see I called setMinDate() method with today's time but it as no effect. The dialog shows dates before today as grayed but selectable.

I also tried to sub-class DatePickerDialog and override onDateChanged as suggested in some stackoverflow answers but without any success.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this method

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

instead of your

@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:

I have also faced this issue that user can select not-selectable dates in Android L 5.0.2. Currently there is bug reported here. It is solved in Android L 5.1.0.

For temporary solution of this issue you can compare selected date with current system date and put some condition based on that. I used this as my workaround

EDIT2:

add onDateSent() method in DatePickerDialogFragment and just check if it's earlier than the date you set in setMinDate(). If so, then just show the DatePickerDialog again.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...