TextInputLayout详解

TextInputLayout是什么

TextInputLayout主要是作为EditText的容器,从而为EditText生成一个浮动的Label,当用户点击EditText的时候,EditText中的hint字符串会自动移到EditText的左上角。

TextInputLayout如何使用

●基本用法

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入用户名"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入邮箱"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

●设置最大字符数及错误提示

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:counterEnabled="true" //设置为true才能显字符数
        app:counterMaxLength="5" //设置最大字符数为5
        app:counterOverflowTextAppearance="@style/HintError" //设置超出字符数后提示文字的颜色,如果不设置默认为@color/colorAccent的颜色
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入用户名"
            android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>

style文件(设置超出字符数的文字提示颜色为红色)

<style name="HintError" parent="TextAppearance.AppCompat">
        <item name="android:textColor">@color/colorRed</item>
</style>

●设置错误提示文字

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:errorEnabled="true" //设置为true
        android:id="@+id/textinputlayout_email"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入邮箱"
            android:id="@+id/et_email"
            android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>

java代码

editText_email=findViewById(R.id.et_email);
        textInputLayout =findViewById(R.id.textinputlayout_email);
        editText_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!RegexUtils.isEmail(charSequence)){
                    textInputLayout.setError("邮箱格式错误");
                    textInputLayout.setErrorEnabled(true);
                }else {
                    textInputLayout.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

●设置密码是否可见

xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        app:errorEnabled="true"
        app:passwordToggleEnabled="true" //设置为true
        android:id="@+id/textinputlayout_password"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入密码"
            android:id="@+id/et_password"
            android:inputType="textPassword"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>

个人博客:https://myml666.github.io

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,350评论 19 139
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,132评论 3 119
  • 今天差点忘了 上午下午都工作了,开会记录的不全,回来什么也记不住,导致写汇报修改了好多 早上做了运动 晚上和售后一...
    Parastoo阅读 181评论 0 0
  • 家人极力督促我学医,刚高考完我不是太理解,以为只是图个安稳与福利,那时的我只想TRY EVERYTHING。可当我...
    光_武阅读 561评论 3 0
  • 打 debug 包流程: git pull 分支最新代码 Android Studio:Build - Gener...
    扬州慢_阅读 2,859评论 4 7

友情链接更多精彩内容