当前位置: 移动技术网 > IT编程>移动开发>Android > TextInputLayout输入框控件的悬浮标签

TextInputLayout输入框控件的悬浮标签

2019年07月24日  | 移动技术网IT编程  | 我要评论

槲寄生txt,最新刑侦电视剧,光荣大地下载

本文实例为大家分享了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) { 
  } 
 }); 

点击打开链接免费

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网