Manual input not saved in Android DatePicker (Dialog)

Implementing DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with these classes:

If you use spinners (+ or -) to change the date, everything works fine. The Change Date or Install Date event is raised, and you can retrieve the values ​​entered by the user.

But when the year is entered manually in the input field (via the keyboard) and by the user, then clicks β€œSave” in the dialog box, no event will be triggered, and you will not receive this manually entered value.

It only works when the user changes something again using the sliders after manually entering it manually. Because when you use the sliders, events are fired.

Is this normal behavior? How can I achieve the desired behavior, namely, that the event is fired when the user enters something manually, and then clicks "Save"?

Thanks in advance!

+4
source share
3 answers

Just clear the focus and the android will set the number with manual input.

eg:

 DatePicker datePicker = fidnViewById(R.id.dp); 

When saving as onClick() , add datePicker.clearFocus() ;

That should work.

+8
source

I had the same problem and the accepted answer really helped me a lot. My situation is slightly different, although I am using DatePickerDialog . Here's how DatePicker worked DatePicker .

Declare variables first and then define them.

 private DatePickerDialog.OnDateSetListener date; private DatePickerDialog mDatePickerDialog; private Calendar myCalendar; // Get the calendar instance myCalendar = Calendar.getInstance(); // Define the date set listener first. date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // Do something with year, monthOfYear and dayOfMonth } }; // Now define the DatePickerDialog mDatePickerDialog = new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); mDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { DatePicker datePicker = mDatePickerDialog.getDatePicker(); // The following clear focus did the trick of saving the date while the date is put manually by the edit text. datePicker.clearFocus(); date.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()); } }); 

Then inside the onClick button

 mDatePickerDialog.show(); 
+1
source

If you look at the Date Selection Example , the DatePickerDialog.OnDateSetListener callback will be received as soon as the user clicks the SET button in the dialog box.

Take a look at the dialog below

enter image description here

Even if you enter the date using the keyboard, the date itself is not accepted until you press the SET button in the dialog box, and that is when the DatePickerDialog.OnDateSetListener callback is called.

0
source

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


All Articles