[英]How to disable TimePickerDialog if DatePickerDialog is cancelled?
I am currently making a to-do-list app.我目前正在制作一个待办事项列表应用程序。 I have a reminder function wherein if a user clicks it, then the datepickerdialog and timepickerdialog are shown.
我有一个提醒 function ,其中如果用户单击它,则会显示 datepickerdialog 和 timepickerdialog。
However, if the user presses 'cancel' on the datepickerdialog, then I don't want to show the timepickerdialog.但是,如果用户在 datepickerdialog 上按下“取消”,那么我不想显示 timepickerdialog。 Any help would be appreciated.
任何帮助,将不胜感激。
Here is my code when the "reminder" button is pressed.这是按下“提醒”按钮时的代码。
//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);
Now in setOnCancelListener for the datepicker dialog, I want to have some code to hide the timepickerdialog.现在在 datepicker 对话框的 setOnCancelListener 中,我想要一些代码来隐藏 timepickerdialog。
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();
}
Actually, it was pretty simple to find the answer.实际上,找到答案非常简单。
I just has to move the "notifydatePickerDialog.setOnCancelListener" below the TimePicker Dialog and add "notifytimePickerDialog.hide();"我只需将“notifydatePickerDialog.setOnCancelListener”移到 TimePicker 对话框下方并添加“notifytimePickerDialog.hide();” to the function.
到 function。
Apparently the date picker dialog's cancel button could not read the timepicker dialog while above it.显然,日期选择器对话框的取消按钮在其上方时无法读取时间选择器对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.