无法在Android 6.0+上关闭AlertDialog

时间:2019-05-24 07:44:22

标签: android alertdialog

Android Studio 3.4

我需要:

  1. 显示AlertDialog
  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不会隐藏。结果显示警报对话框和进度条。

enter image description here

2秒后,进度条将隐藏,并且AlertDialog仍显示:

enter image description here

此处为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?

1 个答案:

答案 0 :(得分:1)

只需在onClick中调用dismiss()

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