【问题标题】:How to close error in TextInputLayout?如何关闭 TextInputLayout 中的错误?
【发布时间】:2016-04-20 11:06:58
【问题描述】:

我将TextInputLayout 用于EditText,如果我调用TextInputLayout.setError() 方法,我会得到错误视图 - 没关系。

<android.support.design.widget.TextInputLayout
        android:id="@+id/text_i_l"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="text"
            android:singleLine="true" />

    </android.support.design.widget.TextInputLayout>

但是当我开始向 EditText 输入数据时,这个错误并没有隐藏。 如何关闭此错误?

【问题讨论】:

标签: android


【解决方案1】:

很简单,要检测TextInputLayout 的变化,添加TextWatcher

TextInputLayout.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            TextInputLayout.setErrorEnabled(false) // disable error
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

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

【讨论】:

  • 我使用 com.google.android.material.textfield.TextInputLayout 和选项 addTextChangedListener() 可用于 TextInputEditText 不在 TextInputLayout 中
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多