Android日期选择器监听器不能工作多次..

时间:2012-11-22 07:03:46

标签: java android eclipse datepicker listeners

我有一个活动,其中我有两个日期选择器,首先我用那些语句初始化它们

    public class birthDate extends Activity{

Calendar c = Calendar.getInstance();
    int currentYear = c.get(Calendar.YEAR); 
    int currentMonth = c.get(Calendar.MONTH);
    int currentDay = c.get(Calendar.DAY_OF_MONTH);
  public void onCreate(Bundle savedInstanceState) 
{

 super.onCreate(savedInstanceState);  

 setContentView(R.layout.birthdate);

    DatePicker birthDayDatePicker,periodDatePicker;
            birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
            periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);
             periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
            {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2,int arg3) {
                    // TODO Auto-generated method stub
                    birthDateCalculations();
                }
            });

            birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub

                    periodDateCalculations();

                    }
            });

    }}

我意识到那些听众只发生过一次,就像我再次点击日期选择器一样,这些声明将不会被应用。 我希望每次点击日期选择器时都有一个激活的监听器。

提前致谢!

2 个答案:

答案 0 :(得分:0)

请在活动的 oncreate 方法中初始化选择器。

答案 1 :(得分:0)

 birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
        periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);

        //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();

                }
        });

}

> Blockquote