Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android alertdialog编辑文本验证_Android_Android Alertdialog - Fatal编程技术网

Android alertdialog编辑文本验证

Android alertdialog编辑文本验证,android,android-alertdialog,Android,Android Alertdialog,我有一个添加了编辑文本的警报对话框。它有正按钮和负按钮。每当我单击肯定/确定按钮时,即使编辑文本为空,对话框也会退出 我想控制这种行为。即;我希望alertdialog仅在edittext不为空时退出 注意:strAuthorDesc是一个字符串变量 下面是单击按钮时执行的代码 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); builder.setT

我有一个添加了编辑文本的警报对话框。它有正按钮和负按钮。每当我单击肯定/确定按钮时,即使编辑文本为空,对话框也会退出

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

注意:strAuthorDesc是一个字符串变量

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

                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();

检查EditText是否为空

检查以下代码以供参考

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();

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

      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();
                }
            }
        });
我希望这有帮助


我在这个链接中发现了与对话框相关的问题ans

Put
builder.setCancelable(false)位于alertDialog builder的定义下方

然后是你的积极/消极按钮

                    builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        strAuthorDesc = descInput.getText().toString();
                        if(strAuthorDesc != null && !strAuthorDesc.equals("")){
关闭对话框后,不要调用任何方法,在 最后的


我使用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();
}

您做了哪些更改?if(strAuthorDesc.matches(“”){dialog.disease();Const.showtoos(“添加了说明”,mActivity);setAuthorDesc();}else{Const.showtoos(“说明不能为空!”,mActivity)那它有用吗?不幸的是它没有试过。没用。只要单击“正”按钮,即使strAuthorDesc为空,对话框也会消失。
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();
}