The bug solution for the OnDateSetListener of DatePickerDialog being called twice
When using DatePickerDialog.OnDateSetListener
it, it was found that the callback was called twice. The reason seems to be a source code problem.
solution:
Determine if the view is displayed
final Context context = MainActivity.this;
Calendar calendar = Calendar.getInstance();
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH);
int d = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
if (view.isShown()) { // 这里判断
Toast.makeText(context, String.format(Locale.CHINA, "(y, m, d) = (%d, %d, %d)", year, month, dayOfMonth), Toast.LENGTH_SHORT).show();
}
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(context, onDateSetListener, y, m, d);
datePickerDialog.show();