android - 在 Android 6.0+ 上,AlertDialog 不会关闭
问题描述
安卓工作室 3.4
我需要:
- 显示警报对话框
- 当点击正面按钮隐藏对话框并显示进度条
- 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?
解决方案
只需调用dismiss()
onClick
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//dialog.cancel(); remove this line
}
推荐阅读
- excel - 将多个单元格值粘贴到单个单元格中
- java - Android多应用程序:如何在一个项目中创建两个应用程序?
- c# - 无法在 C# 中使用 Google.Cloud.Translate.V3 创建 TMX 词汇表
- angular - 剑道对话框在角度单元测试用例中抛出异常
- angular - 如何在 NgRx Effect 中调用另一个调度动作?
- python - 计算 pandas 列中每一行有多少个句子
- javascript - DOMException:无法启动视频源 - chrome 85 更新
- python-3.x - 如何使用 lambda 通过函数传递参数
- react-native - 用 redash 和 reanimated 反应原生捏缩放
- spring-boot-chaos-monkey - spring-boot-chaos-monkey - 在同一应用程序的多个实例上更改 assualts/watcher 配置