How can I capture when time changes in TimePicker if a keyboard is in use?

I have a widget that is a TimePicker that retrieves the time stored in a field in a database.

The fact is that when a user changes the time value in widgets, this is not stored in the database.

So, I came across the setOnTimeChangedListener method, which works like a charm if you only use the plus and minus signs in widgets. It does not commit changes if you use a keyword.

Here is my code:

pickedTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { public void onTimeChanged(TimePicker arg0, int arg1, int arg2) { System.out.println("What time is it? " + String.valueOf(arg0.getCurrentHour()) + ":" + String.valueOf(arg0.getCurrentMinute())); } }); 

I also tried unsuccessfully:

selectedTime.setOnFocusChangeListener and selectedTime.setOnKeyListener .

+1
android timepicker
source share
3 answers

As it turned out, the option set android: AddStatesFromChildren = "true" in the XML file, like:

 <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:addStatesFromChildren="true" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:addStatesFromChildren="true" /> 

Or you can set this by code:

  datePicker.setAddStatesFromChildren(true); timePicker.setAddStatesFromChildren(true); 

I really cannot understand what the meaning of default is, as it is.

+1
source share

This did not work for me when working with a dialog box and using TimePicker.

I read somewhere that this is due to focus, so if you have clearFocus, before rejecting the dialog, it will work.

Something like:

 @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { picker.clearFocus(); lastHour=picker.getCurrentHour(); lastMinute=picker.getCurrentMinute(); String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute); if (callChangeListener(time)) { persistString(time); } } } 
+5
source share

Sikora's answer actually adds more value to this common problem.

If you do not control the change event and do not view the value of your timer from another piece of code (for example, the onClick handler for the standard button), then you need a clear focus to get the real value of the time collector when the user manually enters hours or minutes from the keyboard, rather than using the up or down arrows or scroller depending on whether they are on a cellular or later version.

Thanks Sikore.

+1
source share

All Articles