您现在的位置是:首页 > 文章详情

TextInputLayout详解

日期:2018-08-08点击:359

TextInputLayout是什么

TextInputLayout主要是作为EditText的容器,从而为EditText生成一个浮动的Label,当用户点击EditText的时候,EditText中的hint字符串会自动移到EditText的左上角。

TextInputLayout如何使用

●基本用法

xml布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_marginTop="20dp" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:hint="请输入用户名" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_marginTop="20dp" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:hint="请输入邮箱" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> </LinearLayout> 
img_8a86c878ac8dfbe7e8e6bc17e5557786.gif

●设置最大字符数及错误提示

xml

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_marginTop="20dp" app:counterEnabled="true" //设置为true才能显字符数 app:counterMaxLength="5" //设置最大字符数为5 app:counterOverflowTextAppearance="@style/HintError" //设置超出字符数后提示文字的颜色,如果不设置默认为@color/colorAccent的颜色 android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:hint="请输入用户名" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> 

style文件(设置超出字符数的文字提示颜色为红色)

<style name="HintError" parent="TextAppearance.AppCompat"> <item name="android:textColor">@color/colorRed</item> </style> 
img_8412afbfdd60d662698fb3b9288288fe.gif

●设置错误提示文字

xml

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_marginTop="20dp" app:errorEnabled="true" //设置为true android:id="@+id/textinputlayout_email" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:hint="请输入邮箱" android:id="@+id/et_email" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> 

java代码

editText_email=findViewById(R.id.et_email); textInputLayout =findViewById(R.id.textinputlayout_email); editText_email.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(!RegexUtils.isEmail(charSequence)){ textInputLayout.setError("邮箱格式错误"); textInputLayout.setErrorEnabled(true); }else { textInputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable editable) { } }); 
img_058f86d18c92d4713159a26a011d4b7d.gif

●设置密码是否可见

xml

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_marginTop="20dp" app:errorEnabled="true" app:passwordToggleEnabled="true" //设置为true android:id="@+id/textinputlayout_password" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:hint="请输入密码" android:id="@+id/et_password" android:inputType="textPassword" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> 
img_630f5e70e571d2740d828bd25aa9ea0c.gif

个人博客:https://myml666.github.io

原文链接:https://yq.aliyun.com/articles/665566
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章