TextInputLayout输入框控件的悬浮标签
本文实例为大家分享了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) { } });
点击打开链接免费下载源码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android入门之使用SimpleAdapter实现复杂界面布局
这篇文章主要为大家详细介绍了Android如何使用SimpleAdapter实现复杂的界面布局,文中的示例代码讲解详细,对我们学习Android有一定的帮助,需要的可以参考一下2022-11-11Android基础之使用Fragment适应不同屏幕和分辨率(分享)
以下是对Fragment的使用进行了详细的分析介绍,需要的朋友可以过来参考下2013-07-07Android setButtonDrawable()的兼容问题解决办法
这篇文章主要介绍了Android setButtonDrawable()的兼容问题解决办法的相关资料,需要的朋友可以参考下2017-03-03Android之PreferenceActivity应用详解
为了引入这个概率 首先从需求说起 即:现有某Activity专门用于手机属性设置 那么应该如何做呢2012-11-11
最新评论