如果日期选择器对话框被取消,如何禁用TimePickerDialog?



我目前正在制作一个待办事项列表应用程序。我有一个提醒功能,如果用户单击它,则会显示日期选择器对话框和时间选择器对话框。

但是,如果用户在日期选择器对话框中按"取消",那么我不想显示时间选择器对话框。任何帮助将不胜感激。

这是我按下"提醒"按钮时的代码。

//When Reminder button clicked, show datepickerdialog and timepickerdialog
public void openCalendar(View view) {
final Calendar notifycalendar = Calendar.getInstance();
notifyyear = notifycalendar.get(Calendar.YEAR);
notifymonth = notifycalendar.get(Calendar.MONTH);
notifydayOfMonth = notifycalendar.get(Calendar.DAY_OF_MONTH);

final DatePickerDialog notifydatePickerDialog = new DatePickerDialog(Editor_Activity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int notifyyear, int notifymonth, int notifydayOfMonth) {
String notifyDate;
notifyDate = notifydayOfMonth + "/" + (notifymonth + 1) + "/" + (notifyyear);
notifyDateStringfromCalendar = notifyDate;
}
}, notifyyear, notifymonth, notifydayOfMonth);

现在在日期选择器对话框的setOnCancelListener中,我想有一些代码来隐藏时间选择器对话框。

notifydatePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
String notifyDate;
notifyDate = notifydayOfMonth + "/" + (notifymonth + 1) + "/" + (notifyyear);
notifyDateStringfromCalendar = notifyDate;
}
});
TimePickerDialog notifytimePickerDialog = new TimePickerDialog(Editor_Activity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int notifyhourOfDay, int notifyminute) {
String notifyTime;
notifyTime = notifyhourOfDay + "/" + notifyminute;
notifyTimeStringfromCalendar = notifyTime;    
}
}, notifyhourOfDay, notifyminute, false);

notifytimePickerDialog.show();

//DatePicker Dialog shows with not title as well as minimum date set
notifydatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
notifydatePickerDialog.setTitle("");
notifydatePickerDialog.show();
}

实际上,找到答案非常简单。

我只需要将"notifydatePickerDialog.setOnCancelListener"移动到TimePicker Dialog下方,并将"notifytimePickerDialog.hide((;"添加到函数中。

显然,日期选择器对话框的取消按钮在其上方无法读取时间选择器对话框。

最新更新