创建了自定义布局对话框,但在对话框外部单击时无法关闭对话框 window
Created a custom layout dialog, but not able to dismiss dialog when clicked outside dialog window
**Please help me out where I have done wrong**
I am not able to dismiss dialog when clicked on the outside area on my
screen only dismissed when I press backpress in mobile keys.
Dialog dialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.dialog_child_product_two);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.7f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(true);
*// Views inflated in my dialog to perform some actions on keypress*
Button btnDone = (Button) dialog.findViewById(R.id.txt_done);
btnDone.setOnClickListener(view -> {
// Do some work here after some action performed
dialog.dismiss();
});
dialog.show();
您需要移除
dialog.setCancelable(false);
因为如果您希望在对话框区域外单击时关闭对话框,当您将可取消 属性 设置为 false 时,您希望它如何关闭。
Dialog dialog = new Dialog(mContext);
我试过从这个对话框中删除样式
并添加了以下内容:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
或者
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
两者都工作正常并且能够关闭外部区域点击的对话框。
**Please help me out where I have done wrong**
I am not able to dismiss dialog when clicked on the outside area on my screen only dismissed when I press backpress in mobile keys.
Dialog dialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.dialog_child_product_two);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.7f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(true);
*// Views inflated in my dialog to perform some actions on keypress*
Button btnDone = (Button) dialog.findViewById(R.id.txt_done);
btnDone.setOnClickListener(view -> {
// Do some work here after some action performed
dialog.dismiss();
});
dialog.show();
您需要移除
dialog.setCancelable(false);
因为如果您希望在对话框区域外单击时关闭对话框,当您将可取消 属性 设置为 false 时,您希望它如何关闭。
Dialog dialog = new Dialog(mContext);
我试过从这个对话框中删除样式 并添加了以下内容:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
或者
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
两者都工作正常并且能够关闭外部区域点击的对话框。