How do I detect a cancel click of the datepicker dialog?

后端 未结 12 1790
鱼传尺愫
鱼传尺愫 2020-12-03 06:51

i am using following example of date picker

http://developer.android.com/guide/tutorials/views/hello-datepicker.html

now i want to perform some functionality

相关标签:
12条回答
  • 2020-12-03 07:47

    Since DatePickerDialog is a Dialog, it supports the following:

    setOnCancelListener(OnCancelListener listener)
    setOnDismissListener(OnDismissListener listener)
    

    Using both listeners, you can identify all dismissals of the date picker dialog. The first would answer the question, the latter is for completion.

    0 讨论(0)
  • 2020-12-03 07:49
    public void showDatePicker() {
    
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
    
            DatePickerDialog datePicker = new DatePickerDialog(getActivity(), (view, year1, month1, day1) -> {
                Calendar c1 = Calendar.getInstance();
                c1.set(year1, month1, day1);
                Date date = c1.getTime();
                SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
                String d = formatter.format(date);
    //            btnDatePicker.setText(d);
    //            selectedDate = d;
            }, year, month, day) {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    super.onClick(dialog, which);
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                    } else if (which == DialogInterface.BUTTON_POSITIVE) {
    
                    }
                }
            };
    
            datePicker.getDatePicker().setMinDate(System.currentTimeMillis());
            datePicker.show();
        }
    
    0 讨论(0)
  • 2020-12-03 07:50

    The following code helped me solve an issue with the Cancel button not closing out the Date picker window. Thanks to eSilver for the solution.

    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
                datePickerPlugin.success(new PluginResult(PluginResult.Status.OK, ""), callBackId);
                dateDialog.hide();
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-03 07:51

    I added some additional code to @PeteH great answer (which provides for reacting to user pressing date or cancelling).

    private void showDatePickerDialog() {
        final int[] year = new int[1];
        final int[] month = new int[1];
        final int[] day = new int[1];
        isDataSet = false;  // this is used by the onDismiss handler
    
        // Set initial time period in DatePicker to current month
        Calendar calendarCurrent = Calendar.getInstance();
        month[0] = calendarCurrent.get(Calendar.MONTH);
        day[0] =   calendarCurrent.get(Calendar.DAY_OF_MONTH);
        year[0] =  calendarCurrent.get(Calendar.YEAR);
    
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                MyActivity,
                new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int y, int m, int d) {
                isDataSet =true;
                //user confirms date
                year[0] =y;
                month[0] = m;
                day[0] =d;
    
                //do something with date selected
    
            }
        },
                year[0],
                month[0],
                day[0]
        );
        datePickerDialog.setOnDismissListener(mOnDismissListener);
        datePickerDialog.show();
    }
    private DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
            if (!isDataSet){
    
                //do something if user cancels
    
        }
    };
    
    0 讨论(0)
  • 2020-12-03 07:51

    In this part of the code:

     private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
    
                    public void onDateSet(DatePicker view, int year, 
                                          int monthOfYear, int dayOfMonth) {
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        updateDisplay();
                    }
                };
    

    you have to implement also onClick():

    public void  onClick  (DialogInterface  dialog, int which) {
       if (which == Button.NEGATIVE) {
          //Cancel
       }
    }
    

    API for Button.NEGATIVE

    0 讨论(0)
  • 2020-12-03 07:54

    Here's how I did it:

      DatePickerDialog dialog = new DatePickerDialog(this,
                  mDateSetListener,
                  year, month, day);
    
      dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
           if (which == DialogInterface.BUTTON_NEGATIVE) {
              // Do Stuff
           }
        }
      });
    
    0 讨论(0)
提交回复
热议问题