首页 > 解决方案 > 显示对话框时如何仅使 AlertDialog 的按钮可点击

问题描述

我的 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);

Edit2:嗨,我在另一篇文章中找到了解决方案。刚刚在 onCreate 方法中添加了这些行。

this.setFinishOnTouchOutside(false);

非常感谢所有的帮助者。

标签: javaandroidbuttonandroid-alertdialog

解决方案


  alertDialog.setCancelable(false);
  alertDialog.setCanceledOnTouchOutside(false);

推荐阅读