Android 警报对话框不会关闭

标签 android android-alertdialog

我在某些操作之前创建了 AlertDialog,并且 AlertDialog 不会在操作之后关闭。

public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    LayoutInflater inflater = getActivity().getLayoutInflater();
                    View pbview = inflater.inflate(R.layout.progress_bar, null);
                    builder.setView(pbview);

                    builder.setCancelable(true);
                    builder.create().show();
//do some stuff

builder.create().dismiss();
}

顺便说一句,我的 AlertDialog 没有任何按钮。 我想制作没有按钮的 AlertDialog,并在操作结束时自动关闭。

编辑:我更改了实例名称。

最佳答案

同意@ρяσѕρєя K 创建一个 AlertDialog 实例并将您的代码修改为。

  AlertDialog alert ;

 alert =  dlgAlert.create();
        alert.show();
        //do some stuff

                alert.dismiss();

如果你想要一个自定义对话框,那么你可以使用这样的代码:-

 final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button


        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
      }
    });

关于Android 警报对话框不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20737139/

相关文章:

java - Android 可滚动 AlertDialog 以编程方式

android - 如何删除 Android 中 AlertDialog 标题的默认填充/边距

java - 如何在alertDialog中单击按钮时启动 Activity

android - 创建 AlertDialog API 19 后出现 IllegalStateException

android - 在父 View 中处理触摸但在 subview 中处理单击事件

java - 在非 Activity java 类中使用 Android 的 Fused Location 提供程序,并在主 Activity 类中获取经度和纬度

android - 在 React Native 的胜利图表中找不到方法 google() 错误

android - 如何在Android上的音频播放器中添加停止功能?

android - 渐进式 Web 应用程序 - 迄今为止的所有可用权限

android - 保持AlertDialog的DialogInterface打开