关闭警报对话框而不使用负按钮

时间:2017-11-12 16:14:13

标签: java android alertdialog

我创建了一个AlertDialog,它将刷新活动(当按下 btnConfirm1 时),或者什么也不做,只是关闭(当按下 btnDisconfirm1 时)。一切都与 btnDisconfirm1 分开。如何关闭对话框?

显然,AlertDialog没有解雇或取消方法,但有没有使用否定按钮的另一种方式?问题是,我为这个对话框创建了一个布局文件,我不知道如何在我的xlm文件中放置一个负面按钮。

或者除了AlertDialog之外,我应该使用完全不同的方法吗?谢谢!

    btnClear = (Button) findViewById(R.id.btnClear);
    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder mBuilder2 = new AlertDialog.Builder(ScoreScreen.this);

            View mView2 = getLayoutInflater().inflate(R.layout.dialog_confirm_delete, null);

            Button btnConfirm1=(Button) mView2.findViewById(R.id.btnConfirm1);
            Button btnDisconfirm1=(Button) mView2.findViewById(R.id.btnDisconfirm1);

            btnConfirm1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    player1.setPlayerScore(0);
                    player2.setPlayerScore(0);
                    player3.setPlayerScore(0);
                    player4.setPlayerScore(0);

                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            });

            btnDisconfirm1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //WHAT DO I PUT HERE???
                }
            });
            mBuilder2.setView(mView2);
            AlertDialog dialog = mBuilder2.create();
            dialog.show();
        }
    });

2 个答案:

答案 0 :(得分:1)

首先你应该创建AlertDialog AlertDialog mDialog = mBuilder2.create();

其次,你可以在OnClickListener中删除对话框 mDialog.dismiss();

答案 1 :(得分:0)

您可以将此方法放在Util类中。并使用回调

public interface OnDialogDismiss {
    void onPositiveClick();

    void onNegativeClick();
}

根据您的要求修改它。

public void showDialogForMultipleCallback(Context context, String title, String message, boolean cancellable, String neutralBbtn, String negativeBtn, String positiveBtn, final OnDialogDismiss onDialogDismiss) {
    final AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
    builder1.setTitle(title);
    builder1.setMessage(message);
    builder1.setCancelable(cancellable);
    if (neutralBbtn != null) {
        builder1.setNeutralButton(neutralBbtn, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (alert11 != null)
                    alert11.dismiss();
            }
        });
    }
    if (negativeBtn != null) {
        builder1.setNegativeButton(negativeBtn,
                new OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (alert11 != null)
                    alert11.dismiss();
                    }
                });
    }
    if (positiveBtn != null) {
        builder1.setPositiveButton(positiveBtn, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onDialogDismiss.onPositiveClick();
            }
        });
    }
    alert11 = builder1.create();
    alert11.show();
}