如何让Interface Bundle将以前的日期提供给DatePickerDialog?

时间:2015-09-21 02:00:19

标签: android date android-fragments bundle android-datepicker

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

下次打开对话框时(因为用户想要编辑他们之前选择的日期),我想将用户之前选择的日期显示为默认日期,而不是今天的日期。我已经使用Bundle设置了从片段到活动的接口。此外,不确定我在Fragment的OnCreateDialog中对“年份”的空虚测试是否正常工作。请指教。

部分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);
}

部分活动文件:

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

1 个答案:

答案 0 :(得分:1)

  1. 如果找不到上一个日期,请使用cal.get()

  2. 显示对话框以及今天的日期
  3. 用户选择日期,将EditText设置为所选日期。

  4. 如果找到上一个日期,则将文本从EditText解析为日期格式,使用cal.set()将其设置为当前日期,并使用cal.get()

    显示对话框
  5. 您可以使用EditText方法检查.matches()是否为空。

    例如,

    if(txtDate.getText().toString().matches(""))
             // txtDate was empty