java - 如何在显示对话框时仅单击“警报对话框”按钮

标签 java android button android-alertdialog

我的 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/

相关文章:

java - Eclipse:如何查找继承字段的引用(不是

java - 如果 c 比 b 小得多,找到 a**b % c(a 幂 b 模 c)的最佳方法是什么?

java - 如何在java中使用for循环显示图像

android - 如何在 Android 中检测电子邮件发送/接收事件和/或阅读电子邮件内容?

android - 带文本 x 轴的条形图 - achartengine

c# - 按钮的 Xamarin 自定义渲染器 (iOS)

java - 如何将 HashMap 的所有值都小写?

android - 在后台访问 sim-toolkit 的应用程序

Android:Toast 和 Edit.GetText

css - 如何创建指向 url 的自定义按钮链接