Android 5.0+ 解析(六)TextInputLayout控件

hello,大家好!
今天给大家更新的博客是有关TextInputLayout的简单介绍。


TextInputLayout简介

TextInputLayout是把EditText作为自己子控件的一个布局,当输入文字时,它可以把Hint文字飘到EditText的上方。它解决了当用户点击EditText时Hint的文字消失了而导致用户可能不知道当前输入的内容是什么的问题。


TextInputLayout使用

1.导入依赖包
<span style="font-size:18px;">dependencies {
  compile 'com.android.support:appcompat-v7:22.2.0'
  compile 'com.android.support:design:22.2.0'
}</span>

2.布局文件
<span style="font-size:18px;"><android.support.design.widget.TextInputLayout
          android:layout_width="fill_parent"
          android:id="@+id/til_hint_content"
          app:errorEnabled="true"
          android:layout_height="wrap_content">

        <EditText android:id="@+id/et_hint_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:saveEnabled="false"
            android:maxLength="48"
            android:digits="1234567890qwertyuiopasdfghjklzxcvbnm "
            android:hint="你的提示文字"/>
</android.support.design.widget.TextInputLayout></span>

TextInputLayout添加逻辑判断


<span style="font-size:18px;">et_hint_content.addTextChangedListener(new TextWatcher() {
      @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      //输入不符合设置的字符,hint会变成红色并提醒
      @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        //检查输入是否符合自定义规则
        if (checkType(charSequence.toString())) {
          til_hint_content.setErrorEnabled(true);
          til_hint_content.setError("输入信息有误");
          return;
        } else {
          til_hint_content.setErrorEnabled(false);
        }
      }

      @Override public void afterTextChanged(Editable editable) {

      }
    });</span>




欢迎留言和评论!
博客地址:http://blog.csdn.net/caihongdao123 
下一篇  Android 5.0+ 解析(七)Snackbar控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值