增强EditText--TextInputLayout

该控件继承自linearlayout,里边只能包裹一个控件,EditText或继承自EditText,

在用户输入的时候能将原来的提示文字浮动在控件上边。

使用此控件需要引入依赖

dependencies{
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'

}

下面是简单调用的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="UserName" />

    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="password" />

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

</LinearLayout>

实现的效果

858dbea2c90df49aa575cc590ec1b7be79c.jpg

TextInputLayout 属性说明
属性     说明  
app:Theme设置下划线或其他的颜色属性
android.support.design:counterEnabled是否显示计数器
android.support.design:counterMaxLength设置计数器的最大值,与counterEnabled同时使用
android.support.design:counterTextAppearance计数器的字体样式
android.support.design:counterOverflowTextAppearance输入字符大于我们限定个数字符时的字体样式
android.support.design:errorEnabled是否显示错误信息
android.support.design:errorTextAppearance错误信息的字体样式
android.support.design:hintAnimationEnabled是否显示hint的动画,默认true
android.support.design:hintEnabled是否使用hint属性,默认true
android.support.design:hintTextAppearance设置hint的文字样式(指运行动画效果之后的样式)
android.support.design:passwordToggleDrawable设置密码开关Drawable图片,于passwordToggleEnabled同时使用
android.support.design:passwordToggleEnabled是否显示密码开关图片,需要EditText设置inputType
android.support.design:passwordToggleTint设置密码开关图片颜色
android.support.design:passwordToggleTintMode设置密码开关图片(混合颜色模式),与passwordToggleTint同时使用

下面加入显示计数器的, 

 <android.support.design.widget.TextInputLayout
        android:id="@+id/til_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="UserName" />

    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        app:counterEnabled="true"  //设置为true才能显示字符串
        app:counterMaxLength="8"   //设置为最大字符数为8
                                   //实际运行的时候,不能再布局内部加注释,把注释删掉,运行
        
       android:layout_margin="10dp">
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="password" />

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

加入输入的密码不能小于三位时的错误提示文字

xml代码同上

Java代码如下

        final TextInputLayout password_til = findViewById(R.id.til_password);
        final EditText password_et = findViewById(R.id.et_password);
        password_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length()<=3) {
                    password_til.setError("密码不能小于三位");
                    password_til.setErrorEnabled(true);
                } else {
                    password_til.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

效果如下

1f9b0c66084c12d7467cfc7ec68e50dfa6d.jpg

加入密码, 用户可以手动设置可见

效果如下

0e9d49af665ef4689c39a86a89df19d66af.jpg

c336be71f4363b241722421570374e54fb3.jpg

xml代码如下

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

        app:passwordToggleEnabled="true"  //设置为true 是加入密码手动设置是否可见
                                          //需要EditText设置inputType

        android:layout_margin="10dp">

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="password" />

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

更改获取焦点后,上面Label的颜色/大小等

app:hintTextAppearance="@style/HintAppearance"

通过这个属性修改

style示例

<style name="HintAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#8bc34a</item>
</style>

修改下面横线的颜色

c440ec97a89b2c0f24daf89e2afa0922c01.jpg

直接在工程的color中修改

b74abe549408dd15fa7011cf82483a4f421.jpg

修改错误提示的颜色

通过如下方法修改

app:errorTextAppearance="@style/ErrorAppearance"

 style

<style name="ErrorAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#a2ced1</item>
</style>

需要注意的是,该属性不止改变的是错误文字的颜色、大小,还修改了EditText的的那条横线的颜色。

如果不想在编写TextInputLayout布局的时候都去设置,还可以通过style文件去统一设置,如:

<item name="textColorError">@color/textColorError</item>

如果你设置了errorTextAppearance同时又设置了textColorError,TextInputLayout会优先使用errorTextAppearance配置的颜色。

 

 

下面是注意事项 : 百度出来的感觉挺有道理, 贴在下方了

  1. 添加库的时候注意要加appcompat-v7库,确保可以向后兼容
  2. TextInputLayout下的实际视图层次结构不能保证以XML格式编写视图层次结构。 因此,对于TextInputLayout的子对象EditText(或子类TextInputEditText)想调用getParent()可能不会返回TextInputLayout. 如果您需要直接访问视图,建议设置一个android:id并使用findViewById(int).
  3. 一个TextInputLayout只能套一个EditText(或它的子类TextInputEditText)
  4. 当使用passwordToggleDrawable密码开关图片的时候,EditText end位置的 图标时会被覆盖的,为了保证EditText end 位置Drawable的正常显示,你需要在设置这些Drawables 的相对位置(start/end)为绝对(left/right).(官方文档说的)
  5. 使用了TextInputLayout,和它的setError(),布局所占的位置会变多,设计布局注意留适当的空间
  6. EditText的setError和passwordToggleDrawable的图片会重叠,建议只用TextInputLatyout的setError或者重写EditText的布局,抉择一个吧
  7. TextInputLayout.setError()注意调用setErrorEnabled(false)清空错误信息,不然会一直显示
  8. 建议TextInputLayout只套一个EditText,放其他控件会出现焦点抢占的问题(View的事件分发)
  9. 如果加上字数统计需要在style里加上textErrorColor,否则超过字数会后会闪退。

  10.  

    如果不需要字数统计,且启用错误机制(setErrorEnabled(true)), 不需要加上textErrorColor(不会闪退)系统会提供一个默认的error color。 

    当然可以通过textErrorColor来自定义错误颜色(error color). 
    可以使用更为强大的errorTextAppearance来定义错误颜色,字体大小等属性。如果TextInputLayout 同时 设置了textErrorColor和errorTextAppearance ,只有errorTextAppearance生效.
  11.  

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值