Android AlertDialog重新打开或阻止关闭

时间:2017-05-07 16:46:15

标签: android

我有一个带有两个回调函数的AlertDialog。当用户点击“是”或“否”时,回拨正在呼叫。

 public AlertDialog getMydialog(final Context context, final MyCallback onSuccess,final MyCallback onCancel) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("MSG");
        builder.setCancelable(false);
        builder.setTitle("TITLE");

        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                onSuccess.callback();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                onCancel.callback();
            }
        });

        return builder.create();
    }

我的回调:

myDialog = new MyClass(this).MyDialog(this, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-YES");
        }
    }, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-NO");
        }
    });
myDialog.show();

当我点击NO时,对话框被取消。 但不能再显示。例如:

myDialog = new MyClass(this).MyDialog(this, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-YES");
        }
    }, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-NO");
            myDialog.show() // isnt work
        }
    });
myDialog.show();

当我点击“否”按钮时,有人想要禁用.dismiss吗? 或者重新打开对话框?

谢谢!

1 个答案:

答案 0 :(得分:1)

要阻止Dialog获得解雇,您可以使用setOnShowListener上的AlertDialog并添加您的代码..

final AlertDialog mAlertDialog = getMydialog();  //get Dialog
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button button = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //your code
               //you can call dissmiss later 
            }
        });

        Button button2 = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //your code
               //you can call dissmiss later 
            }
        });
    }
});
mAlertDialog.show();