AlertDialog disappears when touch is outside [Android] [duplicate]

此生再无相见时 提交于 2020-05-08 18:57:25

问题


I'm using an Alert Dialog on my application, but it keeps hiding when the users touches outside it. Here is my code:

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();
    }

}

As you can see, I used

builder.setCancelable(false);

but it still doesn't solve the probem. Can you help me? thanks


回答1:


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

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



回答2:


Add

AlertDialog alertDialog = builder.show();

alertDialog.setCanceledOnTouchOutside(false);

in your code




回答3:


You can override the default function for the Dialogue and assure nothing happens. Should work fine.

@Override
public boolean onTouchEvent(MotionEvent event) {
    // If we've received a touch notification that the user has touched
    // outside the app, finish the activity.
    if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
    // Do Something or not...
        return true;
    }
    return false;
}

Or for better practice:

builder.setCanceledOnTouchOutside(false)


来源:https://stackoverflow.com/questions/42254443/alertdialog-disappears-when-touch-is-outside-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!