java - 显示对话框时如何仅使 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);
非常感谢所有的帮助者。
解决方案
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
推荐阅读
- java - java.lang.ClassNotFoundException:从 12.2.1.0.0 升级时 Weblogic 12.2.1.4.0 中的 org.slf4j.Logger
- azure-functions - 谁在满足 Azure 函数 /admin/functions 错误的请求
- elasticsearch - 俄罗斯词干分析器的错误结果
- android - 将 AOSP 项目导出到 Android Studio
- git - 使用 shell 脚本批量克隆 git 存储库时添加目标文件夹
- php - 如何使用 php 播放在数据库中存储为 BLOB 格式的 AVI、FLV 和 Mp4 视频?
- json - Azure 数据工厂 - 内容类型为 JSON,但响应为 XML
- haskell - 如何构建 Haskell 应用程序?
- php - Symfony 令牌在订阅者中为空
- ios - crash - 重击 @escaping @callee_guaranteed (@unowned CMTime)