博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TextInputLayout
阅读量:6938 次
发布时间:2019-06-27

本文共 1700 字,大约阅读时间需要 5 分钟。

  hot3.png

TextInputLayout

TextInputLayout 是一个布局,仅可以添加一个子View且必须为ExitText。

TextInputLayout 为用户提供了两个比较有意思的方法

1.hint是EditText的一个很好的属性,当用户输入数据后,hint内容将自动消失。使用TextInputLayout用户输入数据后内容将不会直接隐藏,而是上浮继续显示;

2.用户输入信息错误,错误提示将直接显示在输入EditText下发。

示例代码:

布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent">

   <EditText

       android:id="@+id/original_edittext"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_below="@id/section_label"

       android:hint="请输入密码"

       />

   <android.support.design.widget.TextInputLayout

       android:id="@+id/text_input_layout"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_below="@id/original_edittext"

       >

       <EditText

           android:id="@+id/Test_edittext"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           />

   </android.support.design.widget.TextInputLayout>

   <EditText

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_below="@id/text_input_layout"

       android:hint="请输入密码"

       />

</RelativeLayout>

Java代码:

    final TextInputLayout textInputLayout = (TextInputLayout) rootView.findViewById(R.id.text_input_layout);

    textInputLayout.setHint("请输入帐号"); //动态设置hint信息,尝试在布局中进行设置为成功

    textInputLayout.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override

        public void onFocusChange(View v, boolean hasFocus) {

            if (!hasFocus) {

                textInputLayout.setError("格式有误"); //自动设置setErrorEnabled为true

            } else {

                textInputLayout.setErrorEnabled(false); //设置错误信息提示。由于提示信息是需要占据一定位置,因此这里设置错误提示不可用

                textInputLayout.setError(null); //设置错误提示信息为空

            }

        }

    });

转载于:https://my.oschina.net/smuswc/blog/598957

你可能感兴趣的文章
关于set_new_handler(转载)
查看>>
[硕.Love Python] FibonacciHeap(F堆 & 斐波那契堆)
查看>>
java.lang.NoClassDefFoundError: net/tsz/afinal/htt
查看>>
我的友情链接
查看>>
SpringBoot入门之缓存
查看>>
创新=深刻的底层认识+丰富的想象力
查看>>
linux上安装Oracle 11g R2 标准版 64位
查看>>
Windows开关机、远程命令
查看>>
字符串转成整数
查看>>
思科命令学习之第二篇
查看>>
24点运算
查看>>
高通平台信号强度和质量的log过滤
查看>>
Yii使用CPagination分页
查看>>
nagios
查看>>
总结自己常用的Eclipse常用快捷键
查看>>
linux系统启动流程
查看>>
VMware vSphere 6简单部署---VCSA( vCenter Server Appliance)部署
查看>>
Spring MVC如何把全局异常记录到日志中?
查看>>
Mysql创建表过程中报1064错误
查看>>
陈松松:视频营销高手悟透的三个持续赚钱的秘诀
查看>>