alertdialog edittext验证

时间:2018-05-24 09:19:27

标签: android alertdialog android-alertdialog

我有一个添加了编辑文字的警告对话框。它有正面和负面的按钮。每当我单击positive / ok按钮时,即使edittext为空,对话框也会退出。

我想控制这种行为。即;我希望仅当edittext不为空时才会退出alertdialog。

注意:strAuthorDesc是一个String变量。

以下是单击按钮时执行的代码。

                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("Author Desc.");
                final EditText descInput = new EditText(mActivity);
                descInput.setInputType(InputType.TYPE_CLASS_TEXT);
                descInput.setTextColor(ContextCompat.getColor(mActivity, R.color.black));
                descInput.setLines(5);
//                    descInput.setMaxLines(5);
                descInput.setGravity(Gravity.LEFT | Gravity.TOP);
                descInput.setSingleLine(false);
                descInput.setHorizontalScrollBarEnabled(false);
                builder.setView(descInput);

                builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        strAuthorDesc = descInput.getText().toString();
                        if(strAuthorDesc != null && !strAuthorDesc.equals("")){
                            dialog.dismiss();
                            Const.showToast("Description added", mActivity);
                            setAuthorDesc();
                        }else{
                            Const.showToast("Desc. cannot be empty!", mActivity);
                        }
                    }
                });
                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

            alertDialog = builder.create();
            alertDialog.setCancelable(false);
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            alertDialog.show();

4 个答案:

答案 0 :(得分:0)

检查editext是否为空

请查看以下代码以供参考

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}

从您的代码中检查以下更新的代码,它将适合您

                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setTitle("Author Desc.");
                    final EditText descInput = new EditText(mActivity);
                    descInput.setInputType(InputType.TYPE_CLASS_TEXT);
                    descInput.setTextColor(ContextCompat.getColor(mActivity, R.color.black));
                    descInput.setLines(5);
    //                    descInput.setMaxLines(5);
                    descInput.setGravity(Gravity.LEFT | Gravity.TOP);
                    descInput.setSingleLine(false);
                    descInput.setHorizontalScrollBarEnabled(false);
                    builder.setView(descInput);

                    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            strAuthorDesc = scInput.getText().toString();
                           if (strAuthorDesc.matches("")) {
                           dialog.dismiss();
                           Const.showToast("Description added",mActivity);
                           setAuthorDesc();
                           }else{
                              Const.showToast("Desc. cannot be empty!",mActivity);
            }
                        }
                    });
                    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                        }
                    });

                alertDialog = builder.create();
                alertDialog.setCancelable(false);
                alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                alertDialog.show();

答案 1 :(得分:0)

我想我已经弄明白了。现在发生的是默认状态是在按下正或负按钮时关闭对话框,所以我们需要覆盖正按钮然后在那里进行验证。这是下面的代码。

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Author Desc.");
        final EditText descInput = new EditText(this);
        descInput.setInputType(InputType.TYPE_CLASS_TEXT);
        descInput.setTextColor(ContextCompat.getColor(this, android.R.color.black));
        descInput.setLines(5);
//                    descInput.setMaxLines(5);
        descInput.setGravity(Gravity.LEFT | Gravity.TOP);
        descInput.setSingleLine(false);
        descInput.setHorizontalScrollBarEnabled(false);
        builder.setView(descInput);

        builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

            }
        });

        alertDialog = builder.create();
        alertDialog.setCancelable(false);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        alertDialog.show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                descInput.getText().toString();
                if(descInput.getText().toString().equals("")){
                    Log.d("Test","Is empty");


                }else{
                    Log.d("Test","Is Not Empty "+descInput.getText().toString());
                    alertDialog.dismiss();
                }
            }
        });

我希望这有帮助!

我在此链接How to prevent a dialog from closing when a button is clicked

找到了与对话框相关的问题

答案 2 :(得分:0)

builder.setCancelable(false);置于alertDialog构建器的定义之下。

然后是你的正面/负面按钮

                    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        strAuthorDesc = descInput.getText().toString();
                        if(strAuthorDesc != null && !strAuthorDesc.equals("")){
  

解除Dialog后,不要调用任何方法,调用dismiss()   最后一次

                                dialog.dismiss();
                                Const.showToast("Description added", mActivity);
                                setAuthorDesc();
                            }else{
                                Const.showToast("Desc. cannot be empty!", mActivity);
                            }
                        }
                    });
                    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                        }
                    });

答案 3 :(得分:0)

我使用Dialog类而不是AlertDialog / Builder或扩展DialogFragment或Dialog类解决了这个问题。

这是代码。

private void showDescDialog(boolean isEditingDesc, String textToEdit){
    final Dialog dialog = new Dialog(mActivity);
    dialog.setContentView(R.layout.custom_dialog_author_desc);
    dialog.setTitle("");

    TextView tvHeading = (TextView) dialog.findViewById(R.id.tv_heading);
    final EditText descInput = (EditText) dialog.findViewById(R.id.et_author_desc);
    Button btnSave = (Button) dialog.findViewById(R.id.btn_save);
    Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel);

    if(isEditingDesc){
        descInput.setText(textToEdit);
        tvHeading.setText("Edit author description");
    }else{
        tvHeading.setText("Add author description");
    }

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            strAuthorDesc = descInput.getText().toString();
            if(!TextUtils.isEmpty(strAuthorDesc)){
                Const.showToast("Description added", mActivity);
                setAuthorDesc();
                dialog.dismiss();
            }else{
                Const.showToast("Desc. cannot be empty!", mActivity);
            }
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.cancel();
        }
    });

    dialog.setCancelable(false);
    dialog.show();
}