如果 DatePickerDialog 被取消,如何禁用 TimePickerDialog?

How to disable TimePickerDialog if DatePickerDialog is cancelled?

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

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

这是按下 "reminder" 按钮时的代码。

 //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 对话框下方,然后将 "notifytimePickerDialog.hide();" 添加到函数中即可。

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