适用于API的Android AlertDialog setOnDismissListener低于17

适用于API的Android AlertDialog setOnDismissListener低于17

问题描述:

我创建了一个AlertDialog:

I created an AlertDialog:

private CharSequence[] _items = { "item1", "item2", "item3", "item4",
"item5", "item6", "item7" };

AlertDialog.Builder daysBuilder = new AlertDialog.Builder(this);
    daysBuilder.setTitle("SomeCaption");

    daysBuilder.setMultiChoiceItems(_items,new Boolean[] { false, true, false, 
false false false, true }, SetListener);
    daysBuilder.setPositiveButton("OK", OKListener);
    daysBuilder.setNegativeButton("Cancel", CancelListener);

    AlertDialog alert = daysBuilder.create();
    alert.show();`

通过语句 new Boolean [] {false,true,false false false false,true} 默认情况下,对话框中的项目
会被选中/取消选中。

Through the statement "new Boolean[] { false, true, false false false false, true }" the items in the dialog get checked/unchecked by default.

当我打开对话框时,更改项目的选择但随后解除(通过按取消或设备的后退按钮)对话框被取消。到目前为止一切都那么好。

When I open the dialog, change the selection of the items but then dismiss (by pressing cancel or the back-button of the device) the dialog gets dismissed. So far so good.

但是当我重新打开对话框时,项目具有从上一次打开对话框开始的先前更改的选中/未选中状态。

But when I reopen the dialog, the items have the checked/unchecked state of the previous changes from the last opening of the dialog.

但是当对话框在第一次打开时被解除时,我想要检查/取消选中项目状态,就像我创建对话框一样( new Boolean [] {false,true,false false false false,true} )。

But when the dialog was dismissed at the first opening, I want to have the items checked/unchecked state like when I created the dialog (new Boolean[] { false, true, false false false false, true }).

所以基本上我需要有机会在对话被解雇时收到通知所以我可以重置项目的状态。

So basically I need an opportunity to get notified when the dialog gets dissmissed so I can then reset the state of the items.

我用对话框对象的setOnDismissListener尝试了它。不幸的是,它只在API 17中可用。

I tried it with the setOnDismissListener for the dialog object. Unfortunately this is just available in API 17.

setOnDismissListener在模拟器(API 17)中对我(完全是我需要的)工作非常完美,但在我的设备上却没有(Android 4.1) => API 16)

setOnDismissListener worked perfect for me (exactly what I need) in the emulator (API 17) but not on my device (Android 4.1 => API 16)

API 16中是否有类似内容?

Is there something similar in API 16?

问题是您使用的是 setOnDismissListener AlertDialog.Builder 。这是在Api级别17中引入的, setOnDismissListener AlertDialog 本身是自api级别1以来。

The problem is you are using setOnDismissListener of AlertDialog.Builder. This was introduced in Api level 17, setOnDismissListener of AlertDialog itself has been since api level 1.

AlertDialog alert = daysBuilder.create();
alert.setOndismissListener(yourdismisslistener);
alert.show();`