How to reset DatePicker when selecting cancel button

I have a datePicker dialog box in my application. It works great when I select a date. However, if I change the date in the dialog and then wring it out, the original edittext file remains unchanged, as it should, but the date picker still has a date earlier when it was canceled. I would like to make sure that EVERY time I go into the Date Picker, it sets the date from EditText. My code is as follows.

onCreate () Method

// Date Listener fdEtDate.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { showDialog(DATE_DIALOG_ID); return false; } }); 

Further down Activity

 @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, dateSetListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)); case STD_DIALOG_ID: return new TimePickerDialog(this, stdSetListener, stdHH, stdMM, true); case STA_DIALOG_ID: return new TimePickerDialog(this, staSetListener, staHH, staMM, true); } return null; } // the callback received when the user "sets" the date in the dialog private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, monthOfYear); cal.set(Calendar.DATE, dayOfMonth); updateDisplay(); } }; // updates the date in the TextView private void updateDisplay() { fdEtDate.setText( new StringBuilder() .append(Utils.pad(cal.get(Calendar.DATE))).append(Const.SQL_DATE_SEP) .append(Utils.pad(cal.get(Calendar.MONTH)+1)).append(Const.SQL_DATE_SEP) .append(cal.get(Calendar.YEAR))); fdEtStd.setText( new StringBuilder() .append(Utils.pad(stdHH)).append(Const.SQL_TIME_SEP) .append(Utils.pad(stdMM))); fdEtSta.setText( new StringBuilder() .append(Utils.pad(staHH)).append(Const.SQL_TIME_SEP) .append(Utils.pad(staMM))); } 

It seems that a new dialog is created each time, so why is it not initialized every time with the date from fdEtDate (EditText)

Thank you in advance

+5
source share
4 answers

you must use onPrepareDialog to set the start date each time a dialog is displayed.

 @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case START_DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate( mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)) } } 

A dialog is only created the first time it is displayed, and from that point on, it is reused unless you explicitly destroy it with removeDialog ().

If you use onCreateDialog (int) to control the state of your dialogs (as described in the previous section), then every time your dialog is rejected, the state of the Dialog object is saved in Activity. If you decide that you no longer need this object, or it is important that the state is cleared, you must call removeDialog (int). This will remove any internal references to the object, and if the dialog shows, it will reject it.

http://developer.android.com/guide/topics/ui/dialogs.html

+8
source

It's simple.

You can use the following method to reset the calendar

 public void resetDate(myDatePicker){ Calendar now = Calendar.getInstance(); this.myDatePicker.updateDate(now.get(now.YEAR), now.get(now.MONTH), now.get(now.DAY_OF_MONTH)); } 

Thus, it is very easy and works great.

+2
source

Probably because you are changing the values โ€‹โ€‹of your Calendar object in onDateSet . Try checking when the method is called by adding some output to LogCat, for example. Log.e( "MyTag", "onDateSet was called!" ); . Then experiment a bit and see when he was called.

If so, and onDateSet is called when you did not expect this, you must change the time at which you set the calendar.

0
source

To reset the date when canceling, press:

  1. Add onDismissListner to Date Index
  2. Inside onDismissListener call the updateData method

Example:

 datePicker.setOnDismissListener((dialog) -> { datePicker.updateData(Year, Month, Date); } 

Note: You might think that the user clicks Ok , this code will create a problem. Ideally, no. Since you are calling updateData on onDateSet , you will not have any problems.

0
source

Source: https://habr.com/ru/post/1382478/


All Articles