来自另一个 DatePicker 的 DatePicker 的 setMinDate

setMinDate of a DatePicker from another DatePicker

我正在为一家酒店开发一个应用程序(个人学习项目)。在 "Book Now" 页面中,我显示了 2 个 EditText 字段。一个用于入住日期,另一个用于退房日期。

我已经使用 setMinDate()DatePicker 设置为仅显示当前和未来的日期。但我想要的是 Check-out DatePicker 应该显示(Check-in Date + 1)作为可选日期。

例如,如果用户选择入住日期为 24/10/2017,则退房 DatePicker 应显示从 25/10/2017 开始的日期。

BookFragment.java

public void onStart() {
    super.onStart();

    EditText inDate = v.findViewById(R.id.checkInDate);
    inDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            DateDialog dialog = new DateDialog(view);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-In Date");
        }
    });

    EditText outDate = v.findViewById(R.id.checkOutDate);
    outDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText inDateText = v.findViewById(R.id.checkInDate);
            if (TextUtils.isEmpty(inDateText.getText().toString())) {
                Toast.makeText(getContext(), "Select Check-in Date first", Toast.LENGTH_SHORT).show();
            } else {
                DateDialog dialog = new DateDialog(view);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                dialog.show(ft, "Select Check-Out Date");
            }
        }
    });

}

DateDialog.java

@SuppressLint("ValidFragment")
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {

EditText dateText;
int cyear, cmonth, cday;

public DateDialog(View view) {
    dateText = (EditText) view;
}

@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {

    DatePickerDialog date = new DatePickerDialog(getActivity(), this, cyear, cmonth, cday);
    final Calendar c = Calendar.getInstance();
    Date currentDate = c.getTime();
    cyear = c.get(Calendar.YEAR);
    cmonth = c.get(Calendar.MONTH);
    cday = c.get(Calendar.DAY_OF_MONTH);
    date.getDatePicker().setMinDate(currentDate.getTime());
    return date;

}

public void onDateSet(DatePicker view, int year, int month, int day) {
    String date = day + "/" + (month + 1) + "/" + year;
    dateText.setText(date);
}

book.xml

<EditText
        android:id="@+id/checkInDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:focusable="false"
        android:hint="@string/check_in_date"
        android:inputType="date" />

    <EditText
        android:id="@+id/checkOutDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:focusable="false"
        android:hint="@string/check_out_date"
        android:inputType="date" />

由于两个 EditText 都在调用同一个 DateDialog class,我怎样才能获取入住日期并将其用于 setMinDate() 以进行退房?

现在,作为解决方法,我正在做的是从 EditText 获取日期字符串,将其解析为日期,然后对结帐执行相同的操作。当用户单击 "Book" 按钮时,它检查签出是否早于使用

签入
if (odate.before(idate)) {
        outDateText.setError("Check-Out must be after Check-In");
        Toast.makeText(getContext(), "Check-Out Date must be after Check-In Date", Toast.LENGTH_SHORT).show();
        flag = 1;
    }

这是可行的,但我宁愿禁用入住日期之前的日期。

让你的Date

String string_date = "24/10/2017";

然后,将您当前的日期增加一天

Calendar cal = Calendar.getInstance(); 
cal.setTime(string_date); 
cal.add(Calendar.DATE, 1);
checkOutDate = cal.getTimeInMillis();

然后将 minDate 设置为您的 datePicker

datePicker.setMinDate(checkOutDate - 1000);

编辑

在您的 DateDialog class

中创建一个 global long 变量 dateToSet

喜欢 - long dateToSet;

constructor

更改为
public DateDialog(View view) {
dateText = (EditText) view;
}

public DateDialog(View view, long dateToSet) {
dateText = (EditText) view;
//then update the value of dateToSet here
this.dateToSet = dateToSet
}

然后为您的 Editext 1 使用此

else {
            Date currentDate = c.getTime();
            cyear = c.get(Calendar.YEAR);
            cmonth = c.get(Calendar.MONTH);
            cday = c.get(Calendar.DAY_OF_MONTH);
            DateDialog dialog = new DateDialog(view,currentDate.getTime());
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-Out Date");
        }

为了你的 Editext 2 使用这个

//String string_date = "24/10/2017";
//getting current date from editText 1
String string_date = editText.getText().toString();
//check the string must not be null
//then, increase one day to your current date

Calendar cal = Calendar.getInstance(); 
cal.setTime(string_date); 
cal.add(Calendar.DATE, 1);
checkOutDate = cal.getTimeInMillis();
DateDialog dialog = new DateDialog(view,checkOutDate);
FragmentTransaction ft = getFragmentManager().beginTransaction();
            dialog.show(ft, "Select Check-Out Date");

那么你的onCreateDialog一定是

public Dialog onCreateDialog(Bundle savedInstanceState) {

DatePickerDialog date = new DatePickerDialog(getActivity(), this, cyear, cmonth, cday);
final Calendar c = Calendar.getInstance();
date.getDatePicker().setMinDate(dateToSet);
return date;

}

可能对你有帮助