2

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

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

                    }
            });

    }}

我意识到那些监听器只发生了一次,就好像我再次单击日期选择器一样,这些语句将不会被应用。我想要一种方法来让监听器在我每次单击日期选择器时都处于活动状态。

提前致谢 !

4

2 回答 2

0

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

于 2012-11-22T07:08:59.500 回答
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
于 2012-11-27T10:04:51.047 回答