小编给大家分享一下TextInputLayout输入框控件悬浮标签的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体内容如下
TextInputLayout也是5.0以后的效果,想要使用同样需要在build中配置:
dependencies {
compile 'com.android.support:design:23.3.0'
}
TextInputLayout可以用来显示一个提示错误信息,把Hint放到EditText左上方等效果的一个布局;
如果项目中有这类的需求,使用TextInputLayout实现起来非常方便;
使用方法也比较简单,直接用TextInputLayout包裹EditText即可:
<android.support.design.widget.TextInputLayout
android:id="@+id/til_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="@+id/et_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
</android.support.design.widget.TextInputLayout>
但是默认情况下,当你输入文本的时候TextInputLayout只会将Hint移动到左上方,不会有错误提示,错误提示需要我们手动设置:
etUser= (EditText) findViewById(R.id.et_user);
tilUser= (TextInputLayout) findViewById(R.id.til_user);
//添加文本变化监听
etUser.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()>6){
//打开TextInputLayout异常提示
tilUser.setErrorEnabled(true);
//设置TextInputLayout异常提示信息
tilUser.setError("账号最大长度为6");
}else {
//关闭TextInputLayout异常提示
tilUser.setErrorEnabled(false);
}
}
@Override
//输入以后调用
public void afterTextChanged(Editable s) {
}
});
以上是“TextInputLayout输入框控件悬浮标签的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。