我有两个编辑文本框,一个用于办理登机手续,另一个用于签出日期。
我已经为此选择了两个DatePickerDialog,当你点击编辑文本框时会打开它,这很好但是我需要它设置所以当选择一个日期来检查编辑文本框然后检查datepicker应该从入住日期的第二天开始..
,即您的退房日期不能在入住日期之前。
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时,它的日期仍设置为今天。
答案 0 :(得分:0)
我还没有测试,但尝试使用下面的代码,如果它有效,
首先使用calander数据类型从日期获取一个全局变量,如下所示:
Calendar fromDate;
现在从日期开始选择将此日期设置为日期,选择日期如下
// 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();
}
}
});