如何在下次打开DatePicker时将先前选择的日期设置为默认日期?

时间:2015-10-22 03:20:25

标签: android datepicker android-edittext

如果EditText行具有先前选择的日期(长度测试> 0),那么我使用Bundle将该日期传递给DatePicker片段。当我下次单击EditText行再次启动DatePicker时,我希望看到以前选择的日期作为默认日期。但我看到了1899年12月的月历,这一天默认为“1”。我在这里错过了什么?

部分活动文件,在onCreate:

else if (hasFocus && (fEditText.getText().length() > 0) && (savedInstanceState  == null)) {
     // Create a new calendar object
     Calendar c = Calendar.getInstance();
     // Extract the previously entered FEditText date as a string
     String dateStr = fEditText.getText().toString().replace(" ","");
     // Split the string date into parts, year, month and day.
     String[]dateParts = dateStr.split("/");
     // Parse the EditText date here to get year, month and day, and then
     // Set the Calendar date in the object to the EditText date.
     // Month is zero-based so need to subtract one from it.
     c.set(Integer.parseInt(dateParts[2]),Integer.parseInt(dateParts[0])-1,Integer.parseInt(dateParts[1]));
     // Now create a bundle to pass the date to the fragment and into the datepicker
     Bundle argsbundle = new Bundle();
     argsbundle.putInt("year", c.get(Calendar.YEAR));
     argsbundle.putInt("month", c.get(Calendar.MONTH));
     argsbundle.putInt("day", c.get(Calendar.DAY_OF_MONTH));
     // Create a new fragment.
     DatePickerFragment newFragment = new DatePickerFragment();
     // Set the year, month and day data from the bundle to the fragment.
     newFragment.setArguments(argsbundle);
     newFragment.show(getSupportFragmentManager(), "datePicker");
     }

DatePickerFragment:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

Calendar c;
int year = 0, month = 0, day = 0;

public DatePickerFragment() {
}

@Override
public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    // If the argsbundle from the Activity has data (because the user previously selected a date) then
    // set that date in the DatePicker.
    if (getArguments() != null) {
        c = Calendar.getInstance();
        year = getArguments().getInt("year");
        month = getArguments().getInt("month");
        day = getArguments().getInt("day");
        c.set(year, month, day);
    } else { // If the DueDate EditText line is empty (no previously selected date by the user then
        // set today's date into the DatePicker.
        // Calendar class obtains the current date on the device and has fields for
        // each of the parts of the date: day, month and year.
        c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog picker = new DatePickerDialog(getActivity(),
            this, year, month, day);
    picker.getDatePicker().setCalendarViewShown(true);
    picker.setTitle("Select a Due Date");
    return picker;
}

public void onDateSet(DatePicker view, int year, int month, int day) {
     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.YEAR,year);
     calendar.set(Calendar.MONTH,month);
     calendar.set(Calendar.DAY_OF_MONTH,day);
     // Set the selected date into the FEditText line.
     EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
     // Format the selected date.
     String myFormat = "MM/dd/yy";
     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
     txtDate.setText(sdf.format(calendar.getTime()));
} 

0 个答案:

没有答案
相关问题