我的 Android 应用程序上有一个 AlertDialog。当我显示给 AlertDialog 时,我只想在用户单击“确定”按钮时禁用。因为当用户单击“确定”按钮时我重置了屏幕。
我的问题是,当我单击 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);
非常感谢所有的帮助者。
最佳答案
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
关于java - 如何在显示对话框时仅单击“警报对话框”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62569573/