【问题标题】:AlertDialog setOnDismissListener not workingAlertDialog setOnDismissListener 不工作
【发布时间】:2012-09-21 06:27:53
【问题描述】:

我的活动打开一个对话框。当它关闭时,我需要执行函数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();

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:
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();
        .....
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      我必须实现DialogInterface.OnCancelListener 并添加onCancel() 方法。成功了!

      【讨论】:

      • setOnDismissListener 只能从 API 17 获得,我想这是你的问题。
      • @YoannHercouet 实际上,API 级别 1?! developer.android.com/reference/android/app/…
      • 仅用于通过 AlerDialog.Builder 设置。在 AlertDialog 中,它从 API 级别 1 开始。
      【解决方案4】:

      在这种情况下,您应该使用alertDialog.setOnCancelListener(listener),而alertDialog.setOnDismissListenerdismissDialog(id) 一起使用。

      【讨论】:

      • 这不起作用。我不会关闭对话框。我只是希望按下后退按钮来关闭对话框......为什么关闭不会触发?
      • @Override public void onBackPressed() {dismissDialog(id) super.onBackPressed();您是否尝试过按下后退按钮的 setOnCancelListener?
      • 在 OnBackPressed() 函数中创建 alertDialog alerDialog 字段并通过对象取消:dialog.cancel();
      【解决方案5】:

      使用以下代码

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

      【讨论】:

        【解决方案6】:

        我发现了真正的问题。

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

        试试看:)

        【讨论】:

          猜你喜欢
          • 2012-12-05
          • 1970-01-01
          • 1970-01-01
          • 2020-06-13
          • 2013-08-18
          • 2011-09-09
          • 1970-01-01
          • 1970-01-01
          • 2016-10-25
          相关资源
          最近更新 更多