【问题标题】:How to disable TimePickerDialog if DatePickerDialog is cancelled?如果 DatePickerDialog 被取消,如何禁用 TimePickerDialog?
【发布时间】:2020-05-25 01:28:50
【问题描述】:

我目前正在制作一个待办事项列表应用程序。我有一个提醒功能,如果用户单击它,则会显示 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);

现在在日期选择器对话框的 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();
    }

【问题讨论】:

    标签: java android android-studio datepickerdialog timepickerdialog


    【解决方案1】:

    其实,找到答案很简单。

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多