在4.2.2版本上调用系统的DatePickerDialog,发现点击确定时系统调用onDateSet()两次,需要过滤其中的一次。
Calendar mCalendar = Calendar.getInstance(); mCalendar.add(Calendar.DAY_OF_YEAR, 1); final DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { boolean mFired = false; public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) { Log.i("PEW PEW", "Double fire check"); if (mFired == true) { Log.i("PEW PEW", "Double fire occured. Silently-ish returning"); return; } else { //first time mFired mFired = true; } //Normal date picking logic goes here } }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)); dateDialog.show();
参考:https://code.google.com/p/android/issues/detail?id=64895