Android 在edittext中输入值后出现TextInputLayout错误

Android 在edittext中输入值后出现TextInputLayout错误,android,material-design,android-textinputlayout,Android,Material Design,Android Textinputlayout,在EditText中输入一个文本后,如何隐藏TextInputLayout错误。 可能吗 我如何才能做到这一点,或者我在这里做错了什么 代码 layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone); layoutEdtPhone.setErrorEnabled(true); layoutEdtPhone.setError(getString(R.string.ui_no_ph

EditText
中输入一个文本后,如何隐藏
TextInputLayout
错误。 可能吗

我如何才能做到这一点,或者我在这里做错了什么

代码

    layoutEdtPhone =(TextInputLayout)rootView.findViewById(R.id.layoutEdtPhone);
    layoutEdtPhone.setErrorEnabled(true);
    layoutEdtPhone.setError(getString(R.string.ui_no_phone_toast));
    layoutEdtPassword =   (TextInputLayout)rootView.findViewById(R.id.layoutEdtPassword);
    layoutEdtPassword.setErrorEnabled(true);
    layoutEdtPassword.setError(getString(R.string.ui_no_password_toast));

    edtPhone=(EditText)rootView.findViewById(R.id.edtPhone);
    edtPassword=(EditText)rootView.findViewById(R.id.edtPassword);
xml



您可以设置
layouttephone.setErrorEnabled(false)

使用textwatcher这里的链接进一步说明Prithviraj给出的答案,
TextInputLayout
本身不进行验证。它只是一种显示错误或提示的机制。您负责设置/清除错误。下面是你如何做到这一点的。请注意,除了TextChangedListener,当用户跳到第二个编辑文本而不在第一个字段中进行任何修改时,可能还需要OnFocusChangeListener来设置错误

protected void onCreate(Bundle savedInstanceState) {
        //.....

        edtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                validateEditText(s);
            }
        });

        edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    validateEditText(((EditText) v).getText());
                }
            }
        });
    }

    private void validateEditText(Editable s) {
        if (!TextUtils.isEmpty(s)) {
            layoutEdtPhone.setError(null);
        }
        else{
            layoutEdtPhone.setError(getString(R.string.ui_no_password_toast));
        }
    }
}

使用if-else条件,如果条件为true,则设置textinOutlayoutBj.setError(null);如果它为false,则在那里设置错误{
textInputLatout.getEditText().addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() < 1) {
               textInputLayout.setErrorEnabled(true);
                textInputLayout.setError("Please enter a value");
            }

            if (s.length() > 0) {
                textInputLayout.setError(null);
                textInputLayout.setErrorEnabled(false);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
@凌驾 更改前文本之前的公共void(字符序列s、int start、int count、int after){ } @凌驾 public void onTextChanged(字符序列、int start、int before、int count){ 如果(s.长度()<1){ textInputLayout.setErrorEnabled(真); textInputLayout.setError(“请输入一个值”); } 如果(s.长度()>0){ textInputLayout.setError(null); textInputLayout.setErrorEnabled(false); } } @凌驾 公共无效后文本已更改(可编辑){ } });
我用@TextChanged of为我工作,看:

@Bind(R.id.layoutEdtPhone)
TextInputLayout tlayoutEdtPhone;
@Bind(R.id.edtPhone)
EditText edtPhone;

//start ButterKnife (I spent the URL with full description for initilize)

@OnTextChanged(R.id.edtPhone)
public void changedTextOnEditPhone() {
    tlayoutEdtPhone.setError("");
}
如果你想知道巴特奈夫,我写了一篇更详细的帖子,但是是用我的母语写的,也就是说,pt_br

Do

mTextInputLayout.setError(null);
以清除错误消息

一个好的做法可以是检查以下错误的方法:

@Override
public void checkErrorBeforeAction() {

    boolean error = false;

    mTextInputLayout.setError(null);

    if (mEditText.getText().toString().length == 0)) {
        mTextInputLayout.setError("Field empty");
    }
    else if (mEditText.getText().toString().isValid) { // Other condition
        mTextInputLayout.setError("Field is invalid");
    }
    if (!error) {
        // Call action
    }
}

这样,它会在设置新错误消息之前刷新错误消息。

如果要从TextInputLayout中删除错误显示,请使用:-

YourtextInputLayout.setErrorEnabled(false);
否则 如果要从edittextfield中删除错误显示,请使用:-

edittext_id.setError(null);
我建议在Android中使用Textwatcher功能,以便更有效地进行验证处理

例如:


遗憾的是,没有实现这种行为的内置机制。我创建了帮助我的
ViewUtils
类:

public final class ViewUtils {
    private ViewUtils() {}

    public static void resetTextInputErrorsOnTextChanged(TextInputLayout... textInputLayouts) {
        for (final TextInputLayout inputLayout : textInputLayouts) {
            EditText editText = inputLayout.getEditText();
            if(editText != null) {
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

                    }

                    @Override
                    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

                    }

                    @Override
                    public void afterTextChanged(final Editable s) {
                        if(inputLayout.getError() != null) inputLayout.setError(null);
                    }
                });
            }
        }
    }
}
然后您可以在客户端代码中轻松使用它:

ViewUtils.resetTextInputErrorsOnTextChanged(mEmailTextInputLayout, mPasswordTextInputLayout);

这就是我如何使用扩展
TextInputLayout
的自定义类来完成的。与其他答案一样使用
TextWatcher
,但在xml中使用此选项后,无需手动执行任何操作。它为你照顾一切

public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context) {
    super(context);
}

public CustomTextInputLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    initRemoveErrorWatcher();
}

private void initRemoveErrorWatcher() {
    EditText editText = getEditText();
    if (editText != null) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
}
要使用,只需将xml中的
TextInputLayout
替换为以下内容:

<com.example.customViews.CustomTextInputLayout>...
。。。
使用KTX:

textInputLayout.editText?.doOnTextChanged { text, start, count, after ->
    if (text?.any(invalidCharacters.toCharArray()::contains) == true) {
      textInputLayout.error = "Invalid character entered: ${invalidCharacters}"
    } else {
      textInputLayout.error = null
    }
  }

你能发布全部代码吗?我想你使用的是碎片,你使用的是哪个版本?BuildTools版本已过期。我已经使用了5.1.1 22最新版本,是的,它的碎片Taka a看看这里感谢它的工作,但我希望没有textwatcher,类似于它自己的属性。不幸的是,Android不支持使用属性进行验证。但是,有两个库可以帮助您减少一些与验证相关的代码。你可以在这里查看:如果我有10个EditText怎么办?创建一个扩展TextInputLayout的自定义类是一个好方法。检查我的回答为什么需要
设置错误(null)
setErrorEnabled(false)
还不够吗?
<com.example.customViews.CustomTextInputLayout>...
textInputLayout.editText?.doOnTextChanged { text, start, count, after ->
    if (text?.any(invalidCharacters.toCharArray()::contains) == true) {
      textInputLayout.error = "Invalid character entered: ${invalidCharacters}"
    } else {
      textInputLayout.error = null
    }
  }