TextInputLayout.setError() leaves empty space after clearing the error

前端 未结 10 1476
囚心锁ツ
囚心锁ツ 2020-12-08 09:24

I recently used TextInputLayout and it\'s setError() method. The problem I\'m getting is, when I clear the error by calling setError(null)

相关标签:
10条回答
  • 2020-12-08 09:27

    Check out the docs for

    public void setErrorEnabled (boolean enabled)
    

    It says

    Whether the error functionality is enabled or not in this layout. Enabling this functionality before setting an error message via setError(CharSequence), will mean that this layout will not change size when an error is displayed.

    Well based on this, try setting setErrorEnabled(true) before setError(), and, set setErrorEnabled(false) after setError(null).

    0 讨论(0)
  • 2020-12-08 09:28

    Dont use setErrorEnabled(boolean), it just doesnt show up the error from the second time.

    public class MyTextInputLayout extends android.support.design.widget.TextInputLayout {
    
    public MyTextInputLayout(Context context) {
        super(context);
    }
    
    public MyTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    public void setError(@Nullable CharSequence error) {
        super.setError(error);
    
        View layout = getChildAt(1);
        if (layout != null) {
            if (error != null && !"".equals(error.toString().trim())) {
                layout.setVisibility(VISIBLE);
            } else {
                layout.setVisibility(GONE);
            }
        }
    }
    }
    

    Then just setError(errorMessage); or setError(null);

    0 讨论(0)
  • 2020-12-08 09:30

    Method setErrorEnabled(false) will clear the extra space, so call it after setError(null).

    0 讨论(0)
  • 2020-12-08 09:33

    By using mTextInputLayout.setErrorEnabled(false); i have solved this problem

    0 讨论(0)
  • 2020-12-08 09:34

    See this page. Google will release the fix in future support library version. It says,

    If you want to fix it now you can extends the TextInputLayout and override the setErrorEnabled() method, but I cant guarantee the backward compatibility. Because its some danger to change state in TextInputLayout.

    public class TextInputLayout extends android.support.design.widget.TextInputLayout{
    
    
        public TextInputLayout(Context context) {
            super(context);
        }
    
        public TextInputLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void setErrorEnabled(boolean enabled) {
            super.setErrorEnabled(enabled);
            if (enabled) {
                return;
            }
            if (getChildCount() > 1) {
                View view = getChildAt(1);
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 09:37

    This is extension in kotlin solving problem:

    fun TextInputLayout.clearError() {
        error = null
        isErrorEnabled = false
    }
    
    0 讨论(0)
提交回复
热议问题