我在同一个活动中有两个日期选择器,它们相互连接,因为当用户更改一个日期时,另一个日期选择器日期会直接更改 以下是听众
//initialize period date picker with current date and on date change listener
periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
// TODO Auto-generated method stub
Number = 1;
birthDayDatePicker.init(currentYear, currentMonth, currentDay, null);
birthDateCalculations();
}
});
//initialize birth date picker with current date and on date change listener
birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {
@Override
public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
// TODO Auto-generated method stub
Number = 2;
periodDatePicker.init(currentYear, currentMonth, currentDay, null);
periodDateCalculations();
}
});
当调用一个日期选择器侦听器时,我将另一个侦听器转到第二个日期选择器为null,我的问题是,如何使我转向null的侦听器再次激活< / em>
答案 0 :(得分:1)
修改强>
如果为侦听器分配变量,则可以重用它们。你似乎也有变量名冲突。调用OnDateChangedListener
时,会有currentYear/currentMonth/currentDay
个函数参数和类成员变量:
OnDateChangedListener periodListener = null;
OnDateChangedListener birthdayListener = null;
periodListener = new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker periodDatePicker, int newYear, int newMonth, int newDay) {
Number = 1;
birthDayDatePicker.init(currentYear, currentMonth, currentDay, null);
birthDateCalculations();
birthDayDatePicker.init(currentYear, currentMonth, currentDay, birthdayListener);
}
birthdayListener = new OnDateChangedListener () {
@Override
public void onDateChanged(DatePicker birthDayDatePicker, int newYear, int newMonth, int newDay) {
Number = 2;
periodDatePicker.init(currentYear, currentMonth, currentDay, null);
periodDateCalculations();
periodDatePicker.init(currentYear, currentMonth, currentDay, periodListener);
}
});
birthDayDatePicker.init(currentYear, currentMonth, currentDay, birthdayListener);
periodDatePicker.init(currentYear, currentMonth, currentDay, periodListener);
答案 1 :(得分:0)
在类级别创建侦听器,只需在代码中使用它。 癌症的答案是正确的。