Java 如何仅使AlertDialog';显示对话框时,s按钮可单击
我的Android应用程序上有一个AlertDialog。当我向AlertDialog显示时,我只想在用户单击“确定”按钮时禁用。因为我在用户点击“OK”按钮时重置了屏幕 我的问题是当我点击AlertDialog之外屏幕上的某个地方时,对话框正在关闭,但我无法清理屏幕 这是我的密码Java 如何仅使AlertDialog';显示对话框时,s按钮可单击,java,android,button,android-alertdialog,Java,Android,Button,Android Alertdialog,我的Android应用程序上有一个AlertDialog。当我向AlertDialog显示时,我只想在用户单击“确定”按钮时禁用。因为我在用户点击“OK”按钮时重置了屏幕 我的问题是当我点击AlertDialog之外屏幕上的某个地方时,对话框正在关闭,但我无法清理屏幕 这是我的密码 AlertDialog.Builder builder = new AlertDialog.Builder(GameOnePlayer.this, R.style.AlertDialogTheme); //build
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>
builder.setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
编辑2:
嗨,我在另一个帖子上找到了解决方案。
刚刚在onCreate方法中添加了这些行
this.setFinishOnTouchOutside(false)代码>
非常感谢你们的帮助
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
将这一行放在“builder.setView(view)”行下面,这样当您在对话框外部触摸时,或者当您按下后退按钮时,您的对话框不会关闭。您可以在对话框中添加OnCancelListener,以监听对话框取消并执行屏幕重置
builder.setOnCancelListener {
//call your method to reset the screen
}
嗨,谢谢你的快速回答。它不起作用,我仍然可以在对话框的屏幕外单击。@ccanozerr您是否尝试使用builder.setCancelable(false)代码>?是的,他试过了。为什么不添加builder.setCancelable(false)代码>?它还可以与onCancel
和onDismiss
方法相关。你可以写。我试过了,但没用。谢谢你的建议。谢谢。我会试试这些,等我做完后会回来。你好,我来解决这个问题。非常感谢你的建议。解决方案是定义this.setFinishOnTouchOutside(false);在onCreate方法中。
builder.setOnCancelListener {
//call your method to reset the screen
}