我正在开发一个酒店应用程序(个人学习项目)。在“立即预订”页面中,我正在显示2 EditText
字段。一个用于入住日期,另一个用于退房日期。来自另一个DatePicker的DatePicker的setMinDate
我已将DatePicker
设置为仅使用setMinDate()
显示当前和未来日期。但我想要的是,退房DatePicker
应显示(入住日期+ 1)作为可选日期。
因此,例如,如果用户选择入住日期为2017年10月24日,则退房DatePicker
应显示2017年10月25日之前的日期。
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类,我怎么能得到签入日期并使用setMinDate()进行签出?
现在,我正在做的解决方法是从EditText获取日期字符串,将其解析为日期并执行相同的检出操作。当用户点击“预订”按钮,它会检查退房是否早于登记入住使用
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;
}
该做的工作,但我宁愿有禁用日期这是前入住日期。
你得到任何错误 – UltimateDevil
@UltimateDevil没有 –