[英]AlertDialog not dismiss on Android 6.0+
Android Studio 3.4
我需要:
以下代碼段:
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.