如何将 DatePickerDialog 的默认日期从今天更改为用户选择的日期?

How do I change a DatePickerDialog's default date from today's date to a user selected date?

我有一个在片段中打开的 DatePickerDialog。首次打开时,对话框的默认日期显示今天的日期。然后用户选择一个日期。当对话框关闭时,我设置了一个带有用户选择日期的 EditText 行。

下次打开对话框时,我想显示用户之前选择的日期,而不是今天的日期。我应该在 onDateSet 函数中创建一个 Bundle 来保存选定的日期吗?如果是这样,下次打开对话框时如何使用 Bundle 填充对话框?如果该行已经设置了日期,那么在 EditText 行上进行空测试是否有用,以便对话框不会填充今天的日期?

部分 DatePickerFragment 文件:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private int currentyear;
private int currentmonth;
private int currentday;

public DatePickerFragment() {
}

public interface OnDateEnteredListener {
     void OnDateEntered(int year, int month, int day);
}
OnDateEnteredListener mListener;


// Set up a Calendar object that will capture the current date for the DatePickerDialog to use.
Calendar cal = Calendar.getInstance();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Bundle datebundle = this.getArguments();
    currentyear = datebundle.get(Calendar.YEAR);
    currentmonth = datebundle.get(Calendar.MONTH);
    currentday = datebundle.get(Calendar.DAY_OF_MONTH);

if(currentyear != 0){
        DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
        dateDialog.getDatePicker().setCalendarViewShown(true);
        dateDialog.getDatePicker().setSpinnersShown(false);
        dateDialog.setTitle("Select a Due Date");
        return dateDialog;
    }
    else {
        // Create three variables to capture the current date.
        currentyear = cal.get(Calendar.YEAR);
        currentmonth = cal.get(Calendar.MONTH);
        currentday = cal.get(Calendar.DAY_OF_MONTH);
        // Create a DatePickerDialog which is a simple dialog containing a DatePicker.  Use the current date
        // as the default date in the DatePicker.  The new instance of DatePickerDialog is created by passing
        // 5 parameters/arguments to the constructor and returning it.
        DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
        dateDialog.getDatePicker().setCalendarViewShown(true);
        dateDialog.getDatePicker().setSpinnersShown(false);
        dateDialog.setTitle("Select a Due Date");
        return dateDialog;
    }
}


public void onDateSet (DatePicker view,int year, int month, int day) {
     mListener.OnDateEntered(year,month,day);
...

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

部分Activity文件:

public class Activity extends AppCompatActivity implements DatePickerFragment.OnDateEnteredListener {

  int currentyear;
  int currentmonth;
  int currentday;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.input);

    fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);

    fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus && (fListenerEditText.getText().length() == 0)) {
        DialogFragment newFragment = new DatePickerFragment();
        Bundle datebundle = new Bundle();
              datebundle.putInt("YEAR", currentyear);
              datebundle.putInt("MONTH", currentmonth);
              datebundle.putInt("DAY", currentday);
              newFragment.setArguments(datebundle);
              newFragment.show(getSupportFragmentManager(), "datePicker");
           }
        }
    });

  @Override
  public void onDateEntered(int year, int month, int day) {
    Toast toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG);
    toast.show();
    }
}

当用户选择日期时,在对话框关闭后使用以下命令将数据传回您的 Activity(将片段中的数据传回起始 activity):http://developer.android.com/training/basics/fragments/communicating.html

在 activity 中收到返回的值后,在启动 DatePickerFragment 时使用以下内容:

DatePickerFragment datepickerfragment = new DatePickFragment();

Bundle bundle = new Bundle();
bundle.putInt("YEAR", currentyear);
bundle.putInt("MONTH", currentmonth);
bundle.putInt("DAY", currentday);
datepickerfragment.Arguments = bundle;

fragmentmanager.BeginTransaction().Add(R.layout.container, datepickerfragment).Commit();

currentyear、currentmonth 和 currentday 是用户选择的日历值。下次打开对话框时检索对话框片段中的值,如下所示:

int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");

现在您可以将对话框的日期设置为从之前的 DatePickerFragment 的值中获取的年、月、日。

编辑: 要设置值,请在 onCreateDialog(Bundle bundle) 中执行以下操作:

....
int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");

if(year != null){
    return new DatePickerDialog(getActivity(), this, year, month, day);
}else{
    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), this, year, month, day);
}