且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当触摸不在[Android]时,AlertDialog消失

更新时间:2023-01-23 21:23:53

使用 setCanceledOnTouchOutside(false)防止在警报对话框外部触摸时被关闭.

Use setCanceledOnTouchOutside(false) for preventing the dismiss on touching outside of alert dialog.

setCancelable(false)用于防止按下返回按钮时被关闭.

setCancelable(false) is used for preventing the dismiss on pressing the back button.

    @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;
}