安卓:安卓4.1模拟器调用onDateSet两次DatePicker的对话 [英] Android: Android 4.1 Emulator Invoking onDateSet Twice from DatePicker Dialog
问题描述
我的应用程序在我的Android 2.2的模拟器可以正常使用。然后,我决定测试在Android 4.1模拟器。该 DatePickerDialog
看起来有点不同的,由于某种原因,当我在完成,preSS中的 onDateSet()
监听器被调用两次,导致我的应用程序的问题。
My application was working perfectly on my Android 2.2 emulator. I then decided to test on an Android 4.1 emulator. The DatePickerDialog
looks a little different and for some reason when I press on "Done", the onDateSet()
listener gets called twice and causes problems in my application.
我知道这是因为如下图所示,在code中的日志打印两次,每当我点击完成
I know this because the log shown below in the code is printed twice whenever I click on "Done"
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Log.v("onDateSet", "ENTERED");
//rest of code...
}};
的Android 2.2的DatePicker
Android 2.2 DatePicker
安卓4.1的DatePicker
Android 4.1 DatePicker
推荐答案
尝试设置一个布尔检查同一对话框中的双火。类似的东西来:
Try setting a boolean to check for a double fire within the same dialog. Something similar to:
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 1);
final DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
boolean fired = false;
public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
Log.i("PEW PEW", "Double fire check");
if (fired == true) {
Log.i("PEW PEW", "Double fire occured. Silently-ish returning");
return;
} else {
//first time fired
fired = true;
}
//Normal date picking logic goes here
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dateDialog.show();
这双火的问题也发生在一个TimePickerDialog的onTimeSet这种检查也适用于那些。
This double fire issue also happens with onTimeSet of a TimePickerDialog and this check also works for those.
这篇关于安卓:安卓4.1模拟器调用onDateSet两次DatePicker的对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!