警告对话框中的问题保持打开正面按钮验证单击android

时间:2015-02-25 05:59:54

标签: android alertdialog android-alertdialog

我想在正面按钮点击中打开我的警告对话框,我有3个有效的条件我在下面检查它是我的代码

    LayoutInflater li = LayoutInflater.from(getActivity());
                    View promptsView = li.inflate(R.layout.dialog_reset, null);
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog));
                    alertDialogBuilder.setView(promptsView);

                    final EditText mEditText = (EditText) promptsView
                            .findViewById(R.id.editText1);
                    final EditText mEditText2 = (EditText) promptsView
                            .findViewById(R.id.editText2);
                    final EditText mEditText3 = (EditText) promptsView
                            .findViewById(R.id.editText3);

                    alertDialogBuilder
                            .setCancelable(false)

                            .setPositiveButton("SUBMIT",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            // get user input and set it to result
                                            // edit text
                                            mStringOldPass = mEditText.getText().toString().trim();
                                            mStringNewPass = mEditText2.getText().toString().trim();
                                            if (mEditText.getText().toString().trim()
                                                    .equalsIgnoreCase("")) {
                                                mAllMethods.ShowDialog(getActivity(),
                                                        "Validation Error", "Enter Old Password",
                                                        "Ok");
                                            } else if (mEditText2.getText().toString().trim()
                                                    .equalsIgnoreCase("")) {
                                                mAllMethods.ShowDialog(getActivity(),
                                                        "Validation Error", "Enter New Password",
                                                        "Ok");

                                            } else if (mEditText3.getText().toString().trim()
                                                    .equalsIgnoreCase("")) {
                                                mAllMethods.ShowDialog(getActivity(),
                                                        "Validation Error",
                                                        "Enter Confirm Password", "Ok");

                                            }

                                            else if (!mEditText3
                                                    .getText()
                                                    .toString()
                                                    .trim()
                                                    .equalsIgnoreCase(
                                                            mEditText2.getText().toString().trim())) {
                                                mAllMethods.ShowDialog(getActivity(),
                                                        "Validation Error",
                                                        "Enter new password not matching", "Ok");

                                            } else {
                                                if (mAllMethods.check_Internet() == true) {
                                                    mDialogPin.dismiss();
                                                    InputMethodManager imm = (InputMethodManager) getActivity()
                                                            .getSystemService(
                                                                    Context.INPUT_METHOD_SERVICE);
                                                    imm.hideSoftInputFromWindow(
                                                            mEditText3.getWindowToken(), 0);


                                                        new changePass().execute();


                                                } else {
                                                    mAllMethods.ShowDialog(getActivity(),
                                                            "Validation Error",
                                                            "Network not available", "Ok");

                                                }

                                            }
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            dialog.cancel();
                                        }
                                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();

在上面的代码中我检查了正面按钮内的3个验证,所以我的验证工作正常,但我的警报对话框同时关闭,我想在我的验证条件触发时保持打开所以任何想法我怎么能解决它?

2 个答案:

答案 0 :(得分:1)

首先创建一个监听器类

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {

        // Do whatever you want here

        // If tou want to close the dialog, uncomment the line below
        //dialog.dismiss();
    }
}

在显示对话框时写

AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setOnClickListener(new CustomListener(dialog));

答案 1 :(得分:0)

你也可以像这样使用..

 Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "ok button is clicked");

            }
        });