如何仅在 android 中使用按钮关闭警报对话框

How to Make alert dialog close with buttons only in android

我想在用户仅选择框中的可用选项之一时关闭警报对话框,并且在他单击警报对话框周围的褪色区域时不关闭。 那么我怎样才能防止警告对话框以这种方式关闭?

if (totalCount == 10){
        AlertDialog.Builder rateDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.rating_deign, null);
        rateDialog.setView(view);
        final AlertDialog alert = rateDialog.create();
        alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alert.show();

        btn_rate = view.findViewById(R.id.btn_rate);
        close = view.findViewById(R.id.close);


        btn_rate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                totalCount = 12;
                editor.commit();
                alert.cancel();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                totalCount = 0;
                editor.commit();
                alert.cancel();
            }
        });
    }

alert.setCancelable(false)您需要添加。

Sets whether the dialog is cancelable or not. Default is true.