Android AlertDialog setOnDismissListener 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12525304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:52:55  来源:igfitidea点击:

AlertDialog setOnDismissListener not working

androidandroid-alertdialog

提问by lumpawire

My activity opens a dialog. When it closes I need the function ReloadTable()to be executed. So I am trying to use setOnDismissListenerbut its not getting triggered. Could someone please help what I am doing wrong?

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

Thanks!

谢谢!

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();

回答by Zeev G

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();
        .....
    }
}

回答by Java Geek

You have to setOnCancelListener to the AlertDialog.Builder:

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

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialogmenu = false;
            }
        })

回答by Yahor10

In this case you should use alertDialog.setOnCancelListener(listener),and alertDialog.setOnDismissListenerworks with dismissDialog(id).

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

回答by lumpawire

OK...I figured it out myself.

好吧...我自己想通了。

I had to implement DialogInterface.OnCancelListenerand add the onCancel()method. It worked!

我必须实现DialogInterface.OnCancelListener并添加该onCancel()方法。有效!

回答by RogerParis

I found the real problem.

我发现了真正的问题。

You should call .show in the dialog, not in the builder.

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

Try it :)

尝试一下 :)

回答by Nagvadiya Vishal

Use following code

使用以下代码

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     
                    }
                });