生成器。我想在解雇或取消时执行某些操作。所以我使用了setOnDismissListener
和setOnCancelListener
,如下面的代码。但是这种方法都没有检测到何时解雇AlertDialog.Builder
。
private static void openOptionDialog(final Context context, Item[] items, String title, DialogInterface.OnClickListener positiveClick) {
ListAdapter adapter = new ArrayAdapter<Item>(
context, android.R.layout.select_dialog_item, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
Item item = getItem(position);
textView.setText(item.string);
textView.setTextSize(16f);
textView.setCompoundDrawablesWithIntrinsicBounds(item.icon, 0, 0, 0);
textView.setCompoundDrawablePadding(BDevice.getPixelFromDp(context, 15));
return view;
}
};
AlertDialog.Builder builder = AlertUtils.getBuilder(context);
builder.setTitle(title);
builder.setAdapter(adapter, positiveClick);
builder.create().show();
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
WidgetSettings preference = new WidgetSettings(context);
if(preference.canWidgetTooteets()){
preference.clear();
}
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
WidgetSettings preference = new WidgetSettings(context);
if(preference.canWidgetTooteets()){
preference.clear();
}
}
});
}
getBuilder
中的AlertUtils
方法如下,
public static AlertDialog.Builder getBuilder(Context context) {
return new AlertDialog.Builder(context, R.style.Dialog);
}
您能否建议我为此找到解决方案?提前谢谢。
答案 0 :(得分:0)
仅使用 AlertDialog 而不是 AlertDialog.Builder ,并添加 setOnDismissListener ,因为它可以正常工作。
例如
AlertDialog alert = builder.create();
alert.setOnDismissListener(yourDismissListener);
alert.show();
AlertDialog alert = builder.create();
alert.setOnCancelListener(yourCancelListener);
alert.show();
答案 1 :(得分:0)
尝试alert.cancel()
代替alert.dismiss()
并添加OnCancelListener
,如下所示:
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
});