繁体   English   中英

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

[英]How to disable TimePickerDialog if DatePickerDialog is cancelled?

我目前正在制作一个待办事项列表应用程序。 我有一个提醒 function ,其中如果用户单击它,则会显示 datepickerdialog 和 timepickerdialog。

但是,如果用户在 datepickerdialog 上按下“取消”,那么我不想显示 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);

现在在 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();
    }

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

我只需将“notifydatePickerDialog.setOnCancelListener”移到 TimePicker 对话框下方并添加“notifytimePickerDialog.hide();” 到 function。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM