如何防止在DatePickerDialog中选择传递日期(setMinDate无法按预期工作)

时间:2015-02-23 09:12:42

标签: android android-5.0-lollipop android-datepicker datepickerdialog

我发现DatePicker(Dialog)非常令人困惑...... 设置setMinDate()时...显示的日历确实"在视觉上禁用"通过日期(让它们变灰)但这些日期仍然可供选择!我想用户阻止这样做。理想的方法是使通过的日期不可点击/不可选。如果不可能,至少要抓住这个事件(哪一个?onDateChanged?,onSelected?...)并保留一条消息并禁用OK按钮,直到用户选择一个有效的日期。

不幸的是,datepicker(对话框)没有onChangedListener(实际上它有但没有setter)。所以我得到的唯一事件是按下OK按钮。太晚了。

这是我的代码

    dateTextView = (TextView) view.findViewById(R.id.createOrder_date);
    dateTextView.setText(R.string.createOrder_time_now);
    dateTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final Calendar calendarNow = Calendar.getInstance();
            final int yearNow = calendarNow.get(Calendar.YEAR);
            final int monthNow = calendarNow.get(Calendar.MONTH);
            final int dayNow = calendarNow.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(CreateOrderFragment.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(year, monthOfYear, dayOfMonth);

                    if (calendar.before(calendarNow)) {
                        Toast.makeText(CreateOrderFragment.this.getActivity(), getResources().getString(R.string.createOrder_datePicker_selectFutureDate), Toast.LENGTH_SHORT).show();
                        view.updateDate(yearNow, monthNow, dayNow);
                        calendar = calendarNow;
                    }

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    dateTextView.setText(sdf.format(calendar.getTime()));
                }
            }, yearNow, monthNow, dayNow);

            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            datePickerDialog.setTitle(getResources().getString(R.string.createOrder_datePicker_title));
            datePickerDialog.show();

        }
    });

enter image description here

2 个答案:

答案 0 :(得分:1)

当您在onDateSent()中收到日期时,只需检查它是否早于您在setMinDate()中设置的日期。如果是这样,那么只需再次显示DatePickerDialog。

代码:

final long tomorrow = System.currentTimeMillis() + DateUtils.MILLIS_IN_DAY;

private void showDatePickerDialog()
{
    mDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()
    {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, monthOfYear, dayOfMonth);

            //kv If user tries to select date in past (or today)
            if (calendar.getTimeInMillis() < tomorrow)
            {
                //kv Make them try again
                showDatePickerDialog();

                Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
            }
            else
            {
                //success
            }
        }
    }, calendarTwoWeeksInFuture.get(Calendar.YEAR), calendarTwoWeeksInFuture.get(Calendar.MONTH), calendarTwoWeeksInFuture.get(Calendar.DAY_OF_MONTH));

    //kv Set minimum date as tomorrow
    mDatePickerDialog.getDatePicker().setMinDate(tomorrow);

    mDatePickerDialog.show();
}

答案 1 :(得分:0)

我一直在寻找一段时间似乎没有办法在新的datePickerDialog(5+)中做到这一点你可以自己处理它,而不是通过点击或重新打开对话框返回最小/最大值,现在由开发人员完成。