三、TextInputLayout

一、TextInputLayout是对EditText的包装扩展,可以对EditText进行输入检查,输入字符计数,密码隐藏,如下图所示:
效果图.png

当EditText获得焦点时,android:hint指定的字符串会以高亮的颜色显示在EditText的上方,当失去焦点时,又会以灰色的提示样式显示在EditText中,这就是最基本的使用方式:当我们输入过程中仍可以看到EditText所关联的提示;

二、TextInputLayout的基本使用
  • 1.首先导入TextInputLayout的依赖

compile 'com.android.support:design:25.1.1'

  • 2.编写xml布局,将TextInputLayout作为EditText的父布局来使用,如下所示:
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidwanga.serenitynanian.serenityproject.TextInputActivity">

    <android.support.design.widget.TextInputLayout
        android:layout_marginTop="100dp"
        android:id="@+id/username_inputlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="userName" />


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

    <!--app:passwordToggleDrawable="@mipmap/ic_launcher_round"设置自己想要的最右边的图标-->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/password_inputlayout"
        android:layout_width="match_parent"
        app:passwordToggleEnabled="true"
        app:passwordToggleTint="@color/colorPrimary"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:inputType="textPassword"
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password" />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

TextInputEditText控件是design给我们提供的一个右边带图标的控件,继承与LinearLayout,只要在TextInputLayout中设置app:passwordToggleEnabled="true"和在TextInputEditText中同时设置android:inputType="numberPassword"就能显示默认的图标,并且将密码隐藏和显示功能已经实现;此功能也可在代码中动态设置

mPasswordTextInputLayout.setPasswordVisibilityToggleEnabled(true);
mPasswordTextInputLayout.setPasswordVisibilityToggleDrawable(R.mipmap.ic_launcher_round);

注意下TextInputLayout中设置字体样式的属性:

  • 1.app:hintTextAppearance:用来设置悬浮上面的提示文字的外观;
  • 2.app:counterTextAppearance:用来设置右下角提示数字的外观,只有设置了setCounterEnabled为true后才有效果;
  • 3.app:counterOverflowTextAppearance:用来设置超过最大字符限制时悬浮左上提示文字和右下角记录数字的外观;TextInputLayout必须设置了setError之后,并且触发了error之后才有效果;
  • 4.app:errorTextAppearance:用来设置下划线和左下角的外观;只有触发了setError才有效果;
三、具体的设置
  • 3.1 输入检查
    除了在EdiText的左上方提示输入外,TextInputLayout还支持在EditText正右下方提示用户是否输入了不和规则条件的内容,以此来提醒用户;
    代码设置如下:
 mUserNameEditText.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                if(length> MAX_LENGTH ){
                    mUserNameTextInputLayout.setError("Max length is :"+ MAX_LENGTH);
                }else{
                    mUserNameTextInputLayout.setErrorEnabled(false);
                }
            }
        });

在上面的代码中我们监听了EditText的输入内容,然后在输入后触发某个条件,调用TextInputLayout的setError方法来设置提醒的描述;与setError相关的方法如下:

    1. public void setError(@Nullable final CharSequence error)
      设置错误提示的文字,它会被显示在EditText的正下方;
  • 2 . public void setErrorTextAppearance(@StyleRes int resId)
    设置错误提示的文字颜色和大小;
    mUserNameTextInputLayout.setErrorTextAppearance(R.style.TextInputErrorStyle);

<style name="TextInputErrorStyle" parent="AppTheme">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/colorPrimary</item>
</style>

  • 3 . ** public void setErrorEnabled(boolean enabled)
    设置错误提示是否可用;

  • 3.2 输入计数
    TextInputLayout支持输入框中字符实时统计,并在字符长度超过设置的阈值后,改变输入框边栏的颜色和提示文字的颜色;
    具体设置如下:

mUserNameTextInputLayout.setCounterEnabled(true);
mUserNameTextInputLayout.setCounterMaxLength(MAX_LENGTH);

与输入计数功能相关的方法:

  • 1 . public void setCounterEnabled(boolean enabled)
    设置计数功能是否可用;

  • 2 . public void setCounterMaxLength(int maxLength)
    设置计数显示的最大值;

  • 3.3 输入时隐藏密码,或者在输入框右边设置一个图标
    在输入框的右边设置一个开关图标,让用来来选择输入时是否能够隐藏(一般用*显示);TextInputLayout也提供了这样的功能,EditText的inputType为textPassword/textWebPassword/numberPassword时,通过下面的设置:

mPasswordTextInputLayout.setPasswordVisibilityToggleEnabled(true);
   mPasswordTextInputLayout.setPasswordVisibilityToggleDrawable(R.mipmap.ic_launcher_round);

我们也可以通过下面的方法来改变输入框右边的图标和颜色:


设置最右边图标.jpg

github仓库

相关内容:

一、CoordinatorLayout的梳理与使用

二、Toolbar的梳理与使用

三、TextInputLayout的梳理与使用

四、FloatingActionButton的梳理与使用

五、Snackbar的梳理与使用

六、CardView的梳理与使用

七、BottomSheetDialog的梳理与使用

八、TabLayout的梳理与使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容