我的Activity中有代码在片段中启动DatePicker。 DatePickerDialog允许用户选择日期。然后,我希望DatePickerDialog关闭(onDismiss)并显示祝酒词。什么都没发生。我在这里缺少什么?
片段文件:
public class SaveDatePickerFrag extends DialogFragment implements DatePickerDialog.OnDateSetListener {
Calendar c;
int year = 0, month = 0, day = 0;
public SaveDatePickerFrag() {
}
@Override
public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
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);
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);
txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.EditText);
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
}
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
if (getActivity() !=null) {
// The below Toast reminds the user to click Save to proceed.
Toast toast = Toast.makeText(getActivity(), "If done, click the Save button", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
}