单击是选项时,警告对话框不会关闭

问题描述 投票:0回答:1

我想做的是在更改编辑文本字段时实现警报对话框。但是我必须在关闭前多次单击“是”选项

@Override
            public void afterTextChanged(Editable s) {
                try {
                    if (!edtDocument1.getText().toString().trim().equals("")) {
                        int idDocumentTypeSelected1 = documentsTypeList.get(spnDocumentType1.getSelectedItemPosition()).idDocumentType;
                        Customer customerModel = new Customer().getCustomerByDocument(edtDocument1.getText().toString().trim(), idDocumentTypeSelected1);
                        if (customerModel != null) {
                            Toast.makeText(context, getString(R.string.customer_exist), Toast.LENGTH_SHORT).show();
                            setDataCustomersFinded(customerModel);

                            new AlertDialog.Builder(context)
                                    .setTitle(R.string.black_list_client)
                                    .setMessage(getString(R.string.black_list_client_id))
                                    // Specifying a listener allows you to take an action before dismissing the dialog.
                                    // The dialog is automatically dismissed when a dialog button is clicked.
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            backInitial();
                                        }
                                    })
                                    .setIcon(android.R.drawable.ic_dialog_alert)
                                    .show();

                        } else {
                            if(idCustomer != 0) {
                                idCustomer = 0;
                                cleanFormCustomer(false);
                            }
                        }
                    }
                }catch (Exception e){
                    Log.e(TAG, "afterTextChanged "+e);
                }
            }

任何帮助或建议都很好。谢谢

android android-alertdialog
1个回答
0
投票

您需要删除嵌套的onCLick()

.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                   backInitial();
            }
       })

替换为:

.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {                  
            backInitial();                       
       })

在您的示例中,第一次单击时,您将初始化onClick()...,然后第二次单击时,您将调用backInitial()

© www.soinside.com 2019 - 2024. All rights reserved.