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

后端 未结 12 1789
鱼传尺愫
鱼传尺愫 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:32

    Simply, if you are using or extending AppCompatDialogFragment() just override the

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        // write your own code 
    }
    

    That is it :)

    0 讨论(0)
  • 2020-12-03 07:35
    DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
    
    
                }
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
    
                }
            });
            dialog.show();
    
    0 讨论(0)
  • 2020-12-03 07:35

    DatePickerDialog now exposes a onCancel listener.

    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
                // Do whatever you want when the date is selected.
                editText.setText(String.format("%04d-%02d-%02d", year, month+1, day));
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
    
        datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
    
            }
        });
    
    0 讨论(0)
  • 2020-12-03 07:43

    Try this:

    if (resultCode == RESULT_CANCELED) {
        Toast toast = Toast.makeText(this,"Datepicker Canceled", 10000);
        toast.show();
        return;
    }
    
    0 讨论(0)
  • 2020-12-03 07:45

    Sorry for popping up ancient question, but I faced similar problem and wrote simple successor of standard DatePickerDialog with proper behavior. See it in this topic. I hope this helps.

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

    If you want to perform a different action depending on whether the user selected a date or not you can use an onDismiss handler. Be sure to set a Boolean (e.g., "isDataSet") to indicate whether the user selected a date or not. Here's an example:

    // Date Picker Dialog
       public void showDatePickerDialog(View view) {
       int year, month, day;
       isDataSet = false;  // this is used by the onDismiss handler
    
    // Set initial time period in DatePicker to current month
       calendarCurrent = Calendar.getInstance(); 
       month = calendarCurrent.get(Calendar.MONTH);
       day =   calendarCurrent.get(Calendar.DAY_OF_MONTH);
       year =  calendarCurrent.get(Calendar.YEAR);
    
       DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, dateSetListener, year, month, day );
       datePickerDialog.setOnDismissListener(mOnDismissListener);
       datePickerDialog.show();
       datePickerDialog_visible=true;  //indicate dialog is up
     } // [END showDatePickerDialog] 
    
    //onDismiss handler
    private DialogInterface.OnDismissListener mOnDismissListener =
            new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    datePickerDialog_visible=false;  //indicate dialog is cancelled/gone
                    if (isDataSet) {  // [IF date was picked
                        //do something, date now selected
                    } else {
                        //do someething else, dialog cancelled or exited
                    }   
                }
            };
    
    0 讨论(0)
提交回复
热议问题