本文介绍了当触摸不在[Android]时,AlertDialog消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

如您所见,我用过

builder.setCancelable(false);

但是它仍然不能解决问题.你能帮助我吗?谢谢

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

推荐答案

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

这篇关于当触摸不在[Android]时,AlertDialog消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:27