首页 > 解决方案 > 在 Android 6.0+ 上,AlertDialog 不会关闭

问题描述

安卓工作室 3.4

我需要:

  1. 显示警报对话框
  2. 当点击正面按钮隐藏对话框并显示进度条
  3. 2秒后隐藏进度条

这里片段:

  private void showConfirmDialogSendPostalOffice() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.accept_terms_title);
        View customView = AndroidUtil.getLinearLayout(getActivity(), R.layout.terms_dialog);
        builder.setView(customView);
        builder.setPositiveButton(R.string.accept_terms, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dialog.cancel();
                ProgressService.getInstance(getActivity()).showProgress(getActivity(), "",
                        getString(R.string.processing) + ", " + getString(R.string.please_wait) + "...");

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ProgressService.getInstance(getActivity()).closeProgress();
                        Toast.makeText(getActivity(),
                                getActivity().getResources().getString(R.string.your_request_successfully_accepted), Toast.LENGTH_LONG).show();
                    }
                }, 2000);
            }
        });
        builder.setNegativeButton(R.string.cancel, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dialog.cancel();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

这一成功适用于 Android 4.4 和 Android 5.0

但是在 Android 6.0+ 上,当我单击肯定按钮时,AlertDialog 不会隐藏。结果显示警报对话框和进度条。

在此处输入图像描述

2 秒后进度条被隐藏,AlertDialog 仍然显示:

在此处输入图像描述

这里是 AlertDialog 布局的布局 - terms_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:text="@string/accept_terms_body"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/viewTerms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:paddingBottom="8dp"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dp"
        android:text="@string/view_terms"
        android:textSize="16sp" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="8dp" />

</LinearLayout>

为什么在 Android 6.0+ 上单击肯定按钮时不会关闭 AlertDialog?

标签: androidandroid-alertdialog

解决方案


只需调用dismiss()onClick

@Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //dialog.cancel(); remove this line
            }

推荐阅读