TimePickerDialog.onTimeSetListener not called

I use the following code to display TimePickerDialog :

 TimePickerDialog dialog = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { System.out.println("onTimeSet"); } }, 20, 15, true); dialog.setTitle("Test"); dialog.setButton(TimePickerDialog.BUTTON_POSITIVE, "Done", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.out.println("Done"); } }); dialog.setButton(TimePickerDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.out.println("Cancel"); } }); dialog.show(); 

On a device running Android 2.3.7, the onTimeSet method onTimeSet not called ( onClick methods are used). On a device running in 4.2.2, the onTimeSet call onTimeSet called as it should.

What's going on here?

+6
source share
1 answer

TimePickerDialog changed after Android 4.1.1, and there is an error about canceling both TimePickerDialog and DatePickerDialog. Read here first. By default, you do not need to set a positive and negative button. TimePickerDialog and DatePickerDialog handle them for you. Therefore, if this cancellation problem is not important to you, remove the positive and negative button settings. If you delete them in both versions, if the user clicks OK, you will call your onTimeSet method.

But I recommend until this error is fixed using a custom AlertDialog with TimePicker widgets;

  final TimePicker timePicker = new TimePicker(this); timePicker.setIs24HourView(true); timePicker.setCurrentHour(20); timePicker.setCurrentMinute(15); new AlertDialog.Builder(this) .setTitle("Test") .setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d("Picker", timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute()); } }) .setNegativeButton(android.R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d("Picker", "Cancelled!"); } }).setView(timePicker).show(); 
+15
source

All Articles