【发布时间】:2020-10-15 13:40:30
【问题描述】:
我的 Android 应用上有一个 AlertDialog。当我向 AlertDialog 显示时,我只想在用户单击“OKAY”按钮时禁用。因为当用户点击“OKAY”按钮时我重置了屏幕。
我的问题是,当我在 AlertDialog 之外的屏幕上单击某个位置时,对话框正在关闭,但我无法清理屏幕。
这是我的代码;
AlertDialog.Builder builder = new AlertDialog.Builder(GameOnePlayer.this, R.style.AlertDialogTheme);
//builder.setCancelable(true);
View view = LayoutInflater.from(GameOnePlayer.this).inflate(
R.layout.layout_winner_dialog,
(ConstraintLayout)findViewById(R.id.layoutAlertDialogContainer)
);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
//alertDialog.setCanceledOnTouchOutside(false);
view.findViewById(R.id.buttonAlertDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideSystemUI();
clearScreen();
alertDialog.dismiss();
}
});
if(alertDialog.getWindow() != null){
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.show();
我试过 alertDialog.setCanceledOnTouchOutside(false);但它没有用。
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
这是我的 style.xml
我该怎么做?谢谢。
编辑:我尝试了这些建议,但没有奏效。
builder.setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
编辑2: 嗨,我在另一篇文章中找到了解决方案。 刚刚在 onCreate 方法中添加了这些行。
this.setFinishOnTouchOutside(false);
非常感谢您的帮助。
【问题讨论】:
-
为什么不加
builder.setCancelable(false);? -
也可以与
onCancel和onDismiss方法相关。你可以写出来。 -
我试过了,但是没有用。感谢您的建议。
-
谢谢你,我会试试这些,当我完成后会回到这里。
-
嗨,Shalu T D,我解决了这个问题。非常感谢您的建议。解决方案是定义 this.setFinishOnTouchOutside(false);在 onCreate 方法中。
标签: java android button android-alertdialog