-1

我在同一个活动中有两个日期选择器并且它们相互连接,因为当用户更改一个日期时,另一个日期选择器日期直接更改 这里是听众

  //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 的侦听器再次处于活动状态

4

2 回答 2

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);
于 2012-11-27T13:46:11.497 回答
0

在类级别创建侦听器,并在您需要时在代码中使用它。癌症的答案是正确的。

于 2012-11-27T13:46:53.710 回答