我有以下代码:
public void startDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setType("start");
newFragment.show(getSupportFragmentManager(), "startDatePicker");
}
使用 DatePickerFragment
private String type = "none";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(getActivity(), "works?", Toast.LENGTH_LONG).show();
if (type.equals("start")) {
EditText e = (EditText) getActivity().findViewById(
R.id.date_of_task);
e.setText("");
} else {
EditText e = (EditText) getActivity().findViewById(
R.id.deadlineDate);
e.setText("");
}
super.onCancel(dialog);
}
public void setType(String t) {
type = t;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if (type.equals("start")) {
EditText e = (EditText) getActivity().findViewById(
R.id.date_of_task);
e.setText("" + day + "." + (month + 1) + "." + year);
} else {
EditText e = (EditText) getActivity().findViewById(
R.id.deadlineDate);
e.setText("" + day + "." + (month + 1) + "." + year);
}
}
我现在的问题是 onCancel 不起作用。因此,我想在 DialogFragment 上添加一个按钮,它会导致 onCreate 中的操作。如何添加按钮或如何解决此问题。