即使没有调用dismiss(),AlertDialog也会被解雇。

时间:2017-08-31 06:29:33

标签: android alertdialog android-alertdialog

我正在创建一个AlertDialog,即使没有调用dismiss(),它也会被解雇。代码如下:

public static void noInternetDialog(final Context context) {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.alert_title);
    builder.setMessage(R.string.internet_not_available);
    builder.setCancelable(false);
    builder.setPositiveButton(R.string.retryBtnLabel, new AlertDialog.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(context, "Retry Clicked", Toast.LENGTH_SHORT).show();



        }
    });

    builder.setNegativeButton(R.string.alert_negative,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context, "Settings Clicked", Toast.LENGTH_SHORT).show();

                }
            });


    AlertDialog alertDialog = builder.create();
    alertDialog.setCancelable(false);
    try {
        alertDialog.show();
    } catch (Exception ignored) {
    }
}

我不希望在某些情况下解除对话,因此我遇到了麻烦,因为它被解雇了。

1 个答案:

答案 0 :(得分:0)

setPositiveButtonsetNegativeButton设置为AlertDialog.Builde

当按下对话框的正按钮或NegativeButton时,对话框将自动关闭