我正在为选择日期范围创建对话框。我查看它们是两个DatePickers,每个最小和最大日期取决于其他当前选定日期。
这是我的代码的一部分。
public void initialize(OnDateRangeSelectedListener callback, Calendar currentStartDate, Calendar currentEndDate) {
onDateRangeSelectedListener = callback;
if (currentStartDate == null){
this.currentStartDate = Calendar.getInstance();
} else {
this.currentStartDate = currentStartDate;
}
if (currentEndDate == null){
this.currentEndDate = Calendar.getInstance();
} else {
this.currentEndDate = currentEndDate;
}
}
public interface OnDateRangeSelectedListener {
void onTimeRangeSelected(Calendar startDate, Calendar endDate);
}
public void setOnDateRangeSetListener(OnDateRangeSelectedListener callback) {
onDateRangeSelectedListener = callback;
}
DatePicker startDatePicker, endDatePicker;
Calendar currentStartDate;
Calendar currentEndDate;
private DatePicker.OnDateChangedListener dateChangedListener = new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
switch (view.getId()){
case R.id.startDatePicker:
currentStartDate.set(year, monthOfYear, dayOfMonth);
break;
case R.id.endDatePicker:
currentEndDate.set(year, monthOfYear, dayOfMonth);
break;
default:
break;
}
if (startDatePicker != null && endDatePicker != null) {
startDatePicker.setMaxDate(currentEndDate.getTimeInMillis());
endDatePicker.setMinDate(currentStartDate.getTimeInMillis());
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.date_range_picker_dialog, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
tabs = (TabHost) root.findViewById(R.id.tabHost);
setDateRange = (Button) root.findViewById(R.id.bSetDateRange);
startDatePicker = (DatePicker) root.findViewById(R.id.startDatePicker);
startDatePicker.init(currentStartDate.get(Calendar.YEAR),
currentStartDate.get(Calendar.MONTH),
currentStartDate.get(Calendar.DAY_OF_MONTH),
dateChangedListener);
startDatePicker.setMaxDate(currentEndDate.getTimeInMillis());
endDatePicker = (DatePicker) root.findViewById(R.id.endDatePicker);
endDatePicker.init(currentEndDate.get(Calendar.YEAR),
currentEndDate.get(Calendar.MONTH),
currentEndDate.get(Calendar.DAY_OF_MONTH),
dateChangedListener);
endDatePicker.setMinDate(currentStartDate.getTimeInMillis());
当我选择某个日期DatePickers不更新且最大(最小)日期保持不变。
如何选择某天后如何更新我的Datepicker视图。
抱歉我的英语不好)))