AlertDialog setOnDismissListener不起作用

时间:2012-09-21 06:27:53

标签: android alertdialog

我的活动会打开一个对话框。当它关闭时,我需要执行函数ReloadTable()。所以我试图使用setOnDismissListener,但它没有被触发。有人可以帮助我做错了吗?

谢谢!

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.transaction, null);
builder = new AlertDialog.Builder(new ContextThemeWrapper(TransactionsList.this , R.style.dialogwithoutdim));
builder.setView(layout);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new OnDismissListener() {
    public void onDismiss(final DialogInterface dialog) {
        ReloadTable();
    }
});

builder.show();

6 个答案:

答案 0 :(得分:10)

public class MyActivity extends Activity implements DialogInterface.OnCancelListener{
    @Override
    public void onCreate(Bundle state) {
       .....
       alertDialog.setOnCancelListener(this);
       alertDialog.show();
    }
    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.dismiss();
        .....
    }
}

答案 1 :(得分:6)

您必须将setOnCancelListener设置为AlertDialog.Builder:

ctx.fillStyle = somePattern;
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(180, 180);
ctx.save();
ctx.translate(offset, offset);
ctx.stroke();
ctx.restore();

答案 2 :(得分:2)

在这种情况下,您应该使用alertDialog.setOnCancelListener(listener)alertDialog.setOnDismissListener适用于dismissDialog(id)

答案 3 :(得分:2)

好的......我自己弄清楚了。

我必须实施DialogInterface.OnCancelListener并添加onCancel()方法。它奏效了!

答案 4 :(得分:1)

我发现了真正的问题。

您应该在对话框中调用.show,而不是在构建器中调用。

尝试一下:)

答案 5 :(得分:1)

使用以下代码

final AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
                final View dailogView = LayoutInflater.from(MyActivity.this).inflate(R.layout.dialog_layout, null);
                builder.setView(dailogView);
                final AlertDialog dialog=builder.create();
                dialog.show();

DismissListener

 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                   // your code after dissmiss dialog     
                    }
                });