java - 当触摸在外部时 AlertDialog 消失 [Android]

标签 java android android-alertdialog

<分区>

我在我的应用程序上使用了一个警告对话框,但当用户触摸到它外面时它会一直隐藏。 这是我的代码:

public class DialogMessageEnd extends DialogFragment
{
    String winner;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        Snooker_Scoreboard ss = new Snooker_Scoreboard();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(false);
        builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
                .setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent i = new Intent(getContext(),PlayerSelection.class);
                        startActivity(i);
                    }
                });



        // Create the AlertDialog object and return it
        return builder.create();
    }

}

如你所见,我使用了

builder.setCancelable(false);

但还是没有解决问题。 你能帮助我吗?谢谢

最佳答案

使用 setCanceledOnTouchOutside(false) 防止在警告对话框之外触摸时关闭。

setCancelable(false) 用于防止在按下后退按钮时关闭。

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    Snooker_Scoreboard ss = new Snooker_Scoreboard();
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(false);
    builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
            .setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent i = new Intent(getContext(),PlayerSelection.class);
                    startActivity(i);
                }
            });



    // Create the AlertDialog object and return it
    Dialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);         
    return dialog;
}

关于java - 当触摸在外部时 AlertDialog 消失 [Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254443/

相关文章:

java - Spring AOP : Trying to use for logging but getting IllegalArgumentException

android - SQLite 数据库错误 : No such table

android - 检测眼球

android - 显示对话框后是否可以替换 AlertDialog 的 View ?

java - 使用 Java 中的 Tumblr API 发布视频

java - 如何使 GML/JTS Geometry 在 Java 中有效?

java.lang.Exception : java. lang.ArrayIndexOutOfBoundsException:7

java - 如何在不知道图像名称的情况下更改 Android 中按钮的背景图像?

android - 当设备的操作栏/状态栏向下滚动时,警报对话框消失

java - Android AlertDialog 关闭方法不起作用