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

前端 未结 10 1477
囚心锁ツ
囚心锁ツ 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:38

    Then you should override it like so:

    @Override
    public void onAnimationEnd(View view)
    {
        view.setVisibility(GONE); // <-- this is where you make it GONE
    
        updateLabelVisibility(true); 
    }
    

    Or try this i.e. on a button or whatever you are using:

    final Button btn = (Button) findViewById(R.id.btn);
    btn.setVisibility(View.GONE); //<--- makes the button gone
    
    0 讨论(0)
  • 2020-12-08 09:39

    I create a custom view for avoiding repeated code and override setError method.

        public class UserInputView extends TextInputLayout {
    
            public UserInputView(Context context) {
               this(context, null);
            }
    
            public UserInputView(Context context, AttributeSet attrs) {
                this(context, attrs, 0);
            }
    
            public UserInputView(Context context, AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
            }
    
            @Override
            public void setError(@Nullable CharSequence error) {
                boolean isErrorEnabled = error != null;
                setErrorEnabled(isErrorEnabled);
                super.setError(error);
            }
    
         }
    
    0 讨论(0)
  • 2020-12-08 09:50

    The source code of TextInputLayout show the following: If you need to clear the error, just use

    til.setErrorEnabled(false);
    

    This will hide the error text and stretch the bottom space to its standard size.

    In case you need to set the error again, just use

    til.setError("Your text");
    

    which automatically calls til.setErrorEnabled(true) as it assumes you need the error functionality.

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

    The following code works fine

        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) {
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题