无法解雇AlertDialog

时间:2015-10-15 22:14:02

标签: android android-alertdialog

在此之前标记为重复或垃圾邮件我希望您知道我在之前的问题中尝试了所有答案但没有任何效果

我有一个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();
    }

结果:

Result

1 个答案:

答案 0 :(得分:0)

因为无论何时打电话openPinDialog() 它都会为警告对话框创建新对象

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

所以它不起作用,意味着没有取消。

openPinDialog()中只调用一次onCreate()或在班级成员中使用任何其他方法保留引用,并使用该成员检查您的情况。

类似的东西,

Private AlertDialog pinDialog = null;


onCreate()
{
 pinDialog = openPinDialog();
}

并使用喜欢,

if (pinDialog  != null && pinDialog.isShowing()){
   pinDialog.dismiss();
}