我有2个DatePicker,第一个是开始日期,第二个是结束日期。我想两者都有maxDate当前日期(这适用于我的代码),我希望当我更改开始日期时,第二个DatePicker的minDate将更新到此日期(反之亦然)。使用此列表器我处理操作,但第二个DatePicker不会在布局上刷新其minDate。
private DatePicker.OnDateChangedListener startDataListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
endDate.setMinDate(c.getTimeInMillis());
}
};
private DatePicker.OnDateChangedListener endDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
startDate.setMaxDate(c.getTimeInMillis());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startDate= (DatePicker)findViewById(***);
endDate= (DatePicker)findViewById(***);
startDate.setMaxDate(Calendar.getInstance().getTimeInMillis());
startDate.init(year, month, day, startDateListener);
endDate.setMinDate(c.getTimeInMillis()-1000);
endDate.setMaxDate(c.getTimeInMillis());
endDate.init(year, month, day, endDateListener);
}
答案 0 :(得分:2)
来自https://stackoverflow.com/a/19722636
这是因为
setMinDate()
的代码已经检查:if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR) && mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR) return;
简单的解决方法是首先设置不同年份的分钟日期。
使用建议的解决方案并在应用解决方案时避免监听器执行我来到了:
DatePicker startDate, endDate;
Calendar minDate = Calendar.getInstance(), maxDate = Calendar.getInstance();
boolean callFromStartDate = false, callFromEndDate = false;
private DatePicker.OnDateChangedListener startDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Avoid listener execution when there's a forced updated on picker
if(callFromEndDate){
callFromEndDate = false;
return;
}
callFromStartDate = true;
// Force picker update
minDate.set(year - 1, monthOfYear, dayOfMonth);
endDate.setMinDate(minDate.getTimeInMillis()); // Calls onDateChange from the other picker
callFromStartDate = true;
// Update the picker to the desired date
minDate.set(year, monthOfYear, dayOfMonth);
endDate.setMinDate(minDate.getTimeInMillis());
}
};
private DatePicker.OnDateChangedListener endDateListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(callFromStartDate) {
callFromStartDate = false;
return;
}
callFromEndDate = true;
maxDate.set(year + 1, monthOfYear, dayOfMonth);
startDate.setMaxDate(maxDate.getTimeInMillis());
callFromEndDate = true;
maxDate.set(year, monthOfYear, dayOfMonth);
startDate.setMaxDate(maxDate.getTimeInMillis());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDate = (DatePicker) findViewById(R.id.datePicker);
endDate = (DatePicker) findViewById(R.id.datePicker2);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
long initialTimeInMillis = c.getTimeInMillis();
startDate.setMinDate(initialTimeInMillis-2000000000);
startDate.setMaxDate(initialTimeInMillis);
startDate.init(year, month, day, startDateListener);
endDate.setMinDate(initialTimeInMillis-2000000000);
endDate.setMaxDate(initialTimeInMillis);
endDate.init(year, month, day, endDateListener);
}
答案 1 :(得分:1)
这是因为方法setMinDate()有检查
if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)
&& mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR){
return;}
只需设置
即可mPicker.setMinDate(0);
//The Min date you wish to set.
mPicker.setMinDate(new LocalDate().minusWeeks(2)
.toDateTimeAtStartOfDay().getMillis());
由此,新的最低日期已经更改了年份,因此它将设置新的日期。 这对我有用
答案 2 :(得分:0)
只需初始化 DatePickerDialog
个对象 TWICE
或 MULTIPLE
次setMinDate(milisecs)
。
void yourListenerMethod() {
datePickerDialog2 = new DatePickerDialog(getActivity(), date2, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog2.getDatePicker().setMaxDate(System.currentTimeMillis()-1000);
datePickerDialog2.getDatePicker().setMinDate(date.getTime());
}