Android Material Design 之 TextInputLayout

点击这里。原创更详细
TextInputLayout

  1. 当里面的EditText获取焦点后,EditText的hint文字,会移动到EditText的上面做Label
  2. 具有错误提示机制
  3. 字数统计功能 输入反馈、描述
//启用计数器
app:counterEnabled="true"
//最大长度
app:counterMaxLength="50"
//错误提示
app:errorEnabled="true"

修改编辑框上label的颜色大小

app:hintTextAppearance="@style/HintAppearance"

修改错误提示的颜色

app:errorTextAppearance="@style/ErrorAppearance"

修改统计字数的样式

//没有超过最大字数
app:counterTextAppearance="@style/CounterAppearance"
//超过最大字数
app:counterOverflowTextAppearance="@style/CounterOverflowAppearance"

使用TextInputLayout遇到的一些坑:
① 如果加上字数统计需要在style里加上textErrorColor,否则超过字数会后会闪退。

② 如果不需要字数统计,且启用错误机制(setErrorEnabled(true)), 不需要加上textErrorColor(不会闪退)系统会提供一个默认的error color。 当然可以通过textErrorColor来自定义错误颜色(error color). 可以使用更为强大的errorTextAppearance来定义错误颜色,字体大小等属性。如果TextInputLayout 同时 设置了textErrorColor和errorTextAppearance ,只有errorTextAppearance生效.

③ 如果加上字数统计,且同时设置了textErrorColor和errorTextAppearance。
这个时候回出现奇怪的效果,Label和统计的颜色为textErrorColor的颜色。
EditText的横线 和 错误文字提示为errorTextAppearance设置的效果。所以为什么不加上textErrorColor会闪退,因为超过字数后TextInputLayout需要textErrorColor属性设置的颜色。

猜你喜欢

转载自blog.csdn.net/qq_34591917/article/details/80034495