繁体   English   中英

无法在Android 6.0+上关闭AlertDialog

[英]AlertDialog not dismiss on Android 6.0+

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

在此处输入图片说明

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?

只需在onClick中调用dismiss()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM