android TextInputLayout setError 隐藏后不再显示

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

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

此问题具体情况:

    

    

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 setError 隐藏后不再显示)