android edittext seterror,android TextInputLayout setError 隐藏后不再显示

本文探讨了在Android开发中遇到的TextInputLayout使用setError()方法时,错误提示无法正常显示的问题。问题源于特定版本(24.0.0)之前的代码逻辑,导致错误提示在清除后无法再次显示。解决方案是正确地调用setErrorEnabled(false)来清除错误状态。文章还深入分析了setError()函数的源码,解释了导致问题的原因,并提供了修复代码示例。
摘要由CSDN通过智能技术生成

最近研究android的material design中的TextInputLayout发现setError()隐藏之后就无法再显示。

通过源码发现此问题出现在compile 'com.android.support:design:24.0.0'之下, 24.0.0版本已修复此问题

此问题具体情况:

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Password" />

final TextInputLayout user = (TextInputLayout )findViewById(R.id.password);

EditText editText = user.getEditText();

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) {

if(s.length() > 4){

user.setError("Password Error");

}else{

//user.setError("");

user.setErrorEnabled(false);

}

}

@Override

public void afterTextChanged(Editable s) {

}

});

通过上面的代码,则当输入框中的字符长度超过4个时则会在输入框底下显示Password Error提示。但是当删除输入框中输入少于4字符时提示消除,当输入字符再次超过4时就不会显示提示。需要把上面代码改为:

if(s.length() > 4){

user.setError("Password Error");

}else{

user.setError("");

user.setErrorEnabled(false);

}

通过源码发现:

public void setError(@Nullable final CharSequence error) {

if (TextUtils.equals(mError, error)) {

// If we already have the same error, ignore

return;

}

mError = error;

if (!mErrorEnabled) {

if (TextUtils.isEmpty(error)) {

// If error isn't enabled, and the error is empty, just return

return;

}

// Else, we'll assume that they want to enable the error functionality

setErrorEnabled(true);

}

// Only animate if we've been laid out already

final boolean animate = ViewCompat.isLaidOut(this);

mErrorShown = !TextUtils.isEmpty(error);

if (mErrorShown) {

mErrorView.setText(error);

mErrorView.setVisibility(VISIBLE);

if (animate) {

if (ViewCompat.getAlpha(mErrorView) == 1f) {

// If it's currently 100% show, we'll animate it from 0

ViewCompat.setAlpha(mErrorView, 0f);

}

ViewCompat.animate(mErrorView)

.alpha(1f)

.setDuration(ANIMATION_DURATION)

.setInterpolator(AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR)

.setListener(new ViewPropertyAnimatorListenerAdapter() {

@Override

public void onAnimationStart(View view) {

view.setVisibility(VISIBLE);

}

}).start();

}

} else {

if (mErrorView.getVisibility() == VISIBLE) {

if (animate) {

ViewCompat.animate(mErrorView)

.alpha(0f)

.setDuration(ANIMATION_DURATION)

.setInterpolator(AnimationUtils.FAST_OUT_LINEAR_IN_INTERPOLATOR)

.setListener(new

ViewPropertyAnimatorListenerAdapter() {

@Override

public void onAnimationEnd(View view) {

mErrorView.setText(error);

view.setVisibility(INVISIBLE);

}

}).start();

} else {

mErrorView.setVisibility(INVISIBLE);

}

}

}

updateEditTextBackground();

updateLabelState(true);}

函数的首部:

if (TextUtils.equals(mError, error)) {

// If we already have the same error, ignore

return;

}

先判断当前需显示的error是否与上次的相等, 如果相等则不会再显示了。所以我们需要在不显示的时候把mError值为“ ”

compile 'com.android.support:design:24.0.0'版本中的已删除了下面的代码段:

if (TextUtils.equals(mError, error)) {

// If we already have the same error, ignore

return;

}

Android TextInputLayout是一个支持Material Design风格的输入框容器,它可以用来包装任何EditTextEditText的子类,提供了一些功能,如错误提示、计数器、密码可见性切换等。 TextInputLayout的主要功能有: 1.错误提示:当用户输入无效数据时,可以显示错误消息。 2.计数器:可以显示已输入字符的数量和最大字符数量。 3.密码可见性切换:可以在明文和密码模式之间切换。 4.动画效果:包含了一些动画效果,如标签浮动和错误消息上移等。 使用TextInputLayout,需要在XML中将EditText包装在TextInputLayout中,并在TextInputLayout中设置相关属性。例如: ``` <com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout> ``` 在代码中,可以使用以下方法来设置错误消息、计数器等: ``` // 设置错误消息 textInputLayout.setError("Invalid input"); // 显示计数器 textInputLayout.setCounterEnabled(true); textInputLayout.setCounterMaxLength(50); // 密码可见性切换 textInputLayout.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE); ``` 总之,TextInputLayout是一个非常有用的控件,可以提高Android应用程序的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值