问题
For some weird reason, when i click on the positive button as part of the DatePickerDialog, the onDateSet method as part of the DateSetListener does not get invoked ONLY ON SAMSUNG DEVICES.
Here is what i am doing :
DateSetListener _datePickerDialogCallback = new DateSetListener();
DatePickerDialog _datePickerDialog = new DatePickerDialog(context, _datePickerDialogCallback, year, month, days);
_datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, StringUtil.getString(R.string.command_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
_done = true;
}
});
_datePickerDialog.show();
private class DateSetListener implements DatePickerDialog.OnDateSetListener {
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND));
if (_done) {
_view.setText(formatDate(calendar.getTime()));
}
}
}
Any suggestions on why this might be happening would be appreciated. pls. note this is only on SAMSUNG DEVICES
回答1:
It looks like from ICS and above, the callback need not be defined while defining the datePickerDialog. But, the onPositiveButtonClick and onNegativeButtonClick would have to call the callback. something like :
_datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
_done = true;
DatePicker datePicker = _datePickerDialog.getDatePicker();
_datePickerDialogCallback.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
回答2:
I'm not sure, whats wrong with your code, but this is how I did it and it works on my SG2 (I didn't do the setButton thing)
My OnDataSetListener implemented as an inner class:
class DatePickHandler implements OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//do stuff
mDateDialog.hide();
}
}
The creation of my DatePickerDialog in a fragment of my app
mDateDialog = new DatePickerDialog(getActivity(),
new DatePickHandler(), mYear, mMonth, mDay);
Than I open the Dialog inside an onClick() method of a onClickListener()
mDateDialog.show();
edit 26.08.13\
I added the following
mDateDialog.setButton(DialogInterface.BUTTON_POSITIVE, "test text", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("test", "in onclick");
});
This is how it looks now. After I press the "test text" button, my newly created onClick method gets called.

来源:https://stackoverflow.com/questions/18402703/datepickerdialog-ondatesetlistener-does-not-get-a-callback-on-samsung-devices