Dismiss DatePickerDialog on pressing back button

后端 未结 8 1929
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 12:29

On my view I have a button which when pressed pops up a DatePickerDialog. The poppedup dialog has a \"Done\" button. When I press that button the selected date is populated

相关标签:
8条回答
  • 2020-12-19 12:50

    Instead of implementing custom DatePickerDialog, you can implement custom OnClickListener for positive button:

    DatePickerDialog dateDialog = new DatePickerDialog(this, null, year, month, day);
    datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
        getResources().getString(R.string.text_done),
        new OnDoneClickListener(datePickerDialog));
    
    // <...>
    
    private class OnDoneClickListener implements DialogInterface.OnClickListener {
    
            private DatePickerDialog mPickerDialog;
    
            BirthDatePickerDialog(DatePickerDialog mPickerDialog) {
                this.mPickerDialog = mPickerDialog;
            }
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                Calendar cal = Calendar.getInstance();
                DatePicker picker =  mPickerDialog.getDatePicker();
                cal.set(Calendar.YEAR, picker.getYear());
                cal.set(Calendar.MONTH, picker.getMonth());
                cal.set(Calendar.DAY_OF_MONTH, picker.getDayOfMonth());
                spinnerDateBirth.setText(dateFormat.format(cal.getTime()));
            }
    }
    

    Any of back or tap on empty area will dismiss the dialog with doing nothing.

    0 讨论(0)
  • 2020-12-19 12:53

    I struggled with this also, but the solution is very simple. The DialogFragment implements the DialogInterface.OnCancelListener and DialogInterface.OnDismissListener and because DialogInterface.OnCancelListener.onCancel gets called before DialogInterface.OnDismissListener.onDismiss, you can clear the date values in onCancel and call the TransactionActivity.populateSetDate method in onDismiss only if the date values are not 0.

    As an aside: to make the Fragment more standalone, it's good practice that the Fragment defines a public interface that the calling activity must implement, so you can call the populateSetDate method on the interface instead of the activity.

    See your code below for implementation of the onCancel and onDismiss:

    public class SelectDateFragment extends DialogFragment
      implements DatePickerDialog.OnDateSetListener
    {
    
      private int year;
      private int month;
      private int day;
    
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
      }
    
      public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        // Calls a method on the activity which invokes this fragment
        // ((TransactionActivity)getActivity()).populateSetDate(yy, mm+1, dd); 
        year = yy;
        month = mm;
        day = dd;
      }
    
      // Gets called before onDismiss, so we can erase the selectedDate
      @Override
      public void onCancel(DialogInterface dialog) {
        year = 0;
        month = 0;
        day = 0;
      }
    
    
      @Override
      public void onDismiss(DialogInterface dialog) {
        if (year != 0) {
          ((TransactionActivity)getActivity()).populateSetDate(year, month + 1, day);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题