在其他 DatePickerDialog 之上设置一个 DatePickerDialog min day one day

Set one DatePickerDialog min day one day above other DatePickerDialog

我有两个编辑文本框,一个用于签入,另一个用于签出日期。

我为此使用了两个 DatePickerDialog,当您单击编辑文本框时会打开它,它运行良好,但我需要设置它,以便在选择日期时检查编辑文本框然后退房日期选择器应该从入住日期的第二天开始..

即您的退房日期不能早于您的入住日期。

my current code:

private void setDateTimeField() {
        checkInEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    fromDatePickerDialog.show();

                }
            }
        });

        checkOutEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    toDatePickerDialog.show();
                }
            }
        });

        Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                checkInEdit.setText(dayOfMonth + "-" + monthOfYear + "-" + year);

                newDate.add(Calendar.DATE, 1);

                toDatePickerDialog.getDatePicker().setMinDate(newDate.get(Calendar.MILLISECOND));
            }

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


        toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                checkOutEdit.setText(dayOfMonth + "-" + monthOfYear + "-" + year);
            }

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


        fromDatePickerDialog.getDatePicker().setMinDate(newCalendar.get(Calendar.MILLISECOND));
        toDatePickerDialog.getDatePicker().setMinDate(newCalendar.get(Calendar.MILLISECOND));
    }

我添加了行

toDatePickerDialog.getDatePicker().setMinDate(newDate.get(Calendar.MILLISECOND));

fromDatePickerDialog onDateSet 方法中,但这似乎没有坚持,即当我打开 toDatePickerDialog 时,它的日期仍然设置为今天。

我还没有测试过,但如果可以,请尝试下面的代码,

首先取一个全局变量作为起始日期,日历数据类型如下:

       Calendar fromDate;

现在从日期 select 开始,将此 fromDate 设置为 selected 日期如下

     // In your fromDatePickerDialog at onDateSet method 
     fromDate = newDate;

现在将下面的代码更改为时间编辑文本的焦点侦听器

     checkOutEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {

                if(fromDate != null)
                      toDatePickerDialog.setMinDate(fromDate.getTime());

                toDatePickerDialog.show();
            }
        }
    });