Android 无法关闭警报对话框

Android 无法关闭警报对话框,android,android-alertdialog,Android,Android Alertdialog,在这是标记为重复或垃圾邮件之前,我想让你们知道,我已经尝试了前面问题的所有答案,但没有任何效果 我有一个AlertDialog,以防被解雇。这是我的代码: private AlertDialog openPinDialog() { builder = new AlertDialog.Builder(MainActivity.this); View view = getLayoutInflater().inflate(R.layout.dialog_layout,

在这是标记为重复或垃圾邮件之前,我想让你们知道,我已经尝试了前面问题的所有答案,但没有任何效果

我有一个AlertDialog,以防被解雇。这是我的代码:

private AlertDialog openPinDialog() {
        builder = new AlertDialog.Builder(MainActivity.this);
        View view = getLayoutInflater().inflate(R.layout.dialog_layout, (ViewGroup)findViewById(R.id.rootLayout));
        one = (Button)view.findViewById(R.id.button);
        two = (Button)view.findViewById(R.id.button2);
        three = (Button)view.findViewById(R.id.button3);
        four = (Button)view.findViewById(R.id.button4);
        five = (Button)view.findViewById(R.id.button5);
        six = (Button)view.findViewById(R.id.button6);
        seven = (Button)view.findViewById(R.id.button7);
        eight = (Button)view.findViewById(R.id.button8);
        nine = (Button)view.findViewById(R.id.button9);
        zero = (Button)view.findViewById(R.id.buttonzero);
        image1 = (ImageView)view.findViewById(R.id.imageView);
        image2 = (ImageView)view.findViewById(R.id.imageView2);
        image3 = (ImageView)view.findViewById(R.id.imageView3);
        image4 = (ImageView)view.findViewById(R.id.imageView4);
        tv = (TextView)view.findViewById(R.id.changeText);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        builder.setView(view);
        builder.setTitle("Master Password");
        return builder.create();
    }
在我希望它被撤销的地方:

private void retypePassword(){

        checkPinCode.append(String.valueOf(buttonClicked.getText()));
        if (count == 5){
            image1.setImageResource(R.drawable.ic_lens_black_24dp);
        }else if (count == 6){
            image2.setImageResource(R.drawable.ic_lens_black_24dp);
        }else if (count == 7){
            image3.setImageResource(R.drawable.ic_lens_black_24dp);
        }else if (count == 8){
            image4.setImageResource(R.drawable.ic_lens_black_24dp);
            checkPinCode = checkPinCode.delete(0,4);
            pinCode = pinCode.delete(4,8);
            Log.e("1st Try", pinCode.toString());
            Log.e("2nd Try", checkPinCode.toString());
            if (checkPinCode.toString().equals(pinCode.toString())){
                tv.setText(R.string.second_text);
                Toast.makeText(MainActivity.this, "Pins match", Toast.LENGTH_SHORT).show();
            }else {
                if (openPinDialog() != null && openPinDialog().isShowing()){
                    openPinDialog().dismiss();
                }
                tv.setText(R.string.first_text);
                Toast.makeText(MainActivity.this, "Pins don't match", Toast.LENGTH_SHORT).show();

                count = 0;
                image1.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp);
                image2.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp);
                image3.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp);
                image4.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp);
                pinCode.delete(0, pinCode.length());
                checkPinCode.delete(0, checkPinCode.length());

            }
        }
    }
onCreate方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        openButton = (Button)findViewById(R.id.button10);
        openButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openPinDialog();
            }
        });
        pinCode = new StringBuffer();
        checkPinCode = new StringBuffer();
    }
结果是:


因为无论何时调用openPinDialog,它都会为警报对话框创建新对象

您正在检查新创建对话框的不同对象上的所有三个条件

所以它不会起作用,意味着不会取消

在onCreate或任何其他方法中只调用openPinDialog一次,并在类级成员中保持引用,并使用该成员检查您的条件

大概

Private AlertDialog pinDialog = null;


onCreate()
{
 pinDialog = openPinDialog();
}
使用类似

if (pinDialog  != null && pinDialog.isShowing()){
   pinDialog.dismiss();
}
视图=LayoutInflater.frombuilder.getContext.inflateR.layout.dialog\u布局,null,false;使用对话框生成器的上下文进行适当的主题化,对话框还并没有根视图。