1

I have implemented a DatePicker dialog using the following code:

static final int DATE_PICKER_FROM = 0;
DatePickerDialog.OnDateSetListener from_dateListener;
private EditText editDateStart;
...
private void addListenersToButtons(){
    editDateStart = (EditText)findViewById(R.id.editDateStart);

    editDateStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_PICKER_FROM);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    switch (id) {
    case DATE_PICKER_FROM:
        return new DatePickerDialog(this, from_dateListener,2,10,2013);
    }
    return null;
}

The following is the inizialization of the listener:

 from_dateListener = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                    int year = selectedYear;
                    int month = selectedMonth;
                    int day = selectedDay;

            editDateStart.setText(
                    new StringBuilder()
                        .append(pad(day)).append("/")
                        .append(pad(month + 1)).append("/")
                        .append(year).append(" "));
            }
        };

When the EditText field is clicked the Dialog showed is the following:

enter image description here

So, the date I tried to set using the code:

DatePickerDialog(this, from_dateListener,2,10,2013);

does not appears. As you can see the date is 1 gen 1900.

Another problem occurs when I try to change the day in the datepicker. For example I have setted from 1 to 2 and the result is the following:

enter image description here

The calendar has reached the date 2 feb 2036. Why?

NOTE: If I change the year in the datepicker from 1900 to 1901 the calendar year change to 2037, but If I set the date to 1902 in the datepicker, the year in calendar become 1902 (and so on)

4

2 回答 2

1

Please read the documentation of DatePickerDialog constructor. The parameters are as follows:

public DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

Parameters

context The context the dialog is to run in.

callBack How the parent is notified that the date is set.

year The initial year of the dialog.

monthOfYear The initial month of the dialog.

dayOfMonth The initial day of the dialog.

So the call should be:

DatePickerDialog(this, from_dateListener, 2013, 10, 2);
于 2013-09-12T09:45:08.730 回答
1

Do some thing like this

DatePickerDialog dp = new DatePickerDialog(mContext, datePickerListener, Year, Month, Day);
于 2013-09-12T09:57:35.533 回答