效果图:

密码使用的是增强文本输入类型,当密码长度小于6或者密码长度大于10的时候就会给出提示。

main.xml

当添加TextInputLayout时,旁边会有一个下载符号,如果点不动,可以右键点击add to design,然后它会加载,加载完毕后,后面那个下载符号就消失了,可以拖动它到相应位置了。

然后可以在MainActivity.java中设置当状态改变时需要进行的操作:

  1. textInputEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  2.  
  3. }
  4.  
  5. @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
  6.  
  7. }
  8.  
  9. @Override
    public void afterTextChanged(Editable s) {
    if(textInputEditText.getText().toString().length()>textInputLayout.getCounterMaxLength()|
    textInputEditText.getText().toString().length()<6){
    textInputLayout.setError("密码长度为6~10位");
    }
    else{
    textInputLayout.setError("");
    }
  10.  
  11. }
    });

beforeTextChanged是状态改变前,onTextChanged是状态改变中,afterTextChanged是状态改变后,我们一般用最后一个。其中getCounterMaxLength的大小是在main.xml中设置的,我设置的是10.

MainActivity.java

  1. package com.example.aimee.textinputlayouttest;
  2.  
  3. import android.support.design.widget.TextInputEditText;
  4. import android.support.design.widget.TextInputLayout;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11. TextInputLayout textInputLayout;
  12. TextInputEditText textInputEditText;
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. textInputLayout=findViewById(R.id.textinputlayout1);
  19. textInputEditText=findViewById(R.id.textinputedit1);
  20. check();
  21. }
  22.  
  23. private void check(){
  24. textInputEditText.addTextChangedListener(new TextWatcher() {
  25. @Override
  26. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  27.  
  28. }
  29.  
  30. @Override
  31. public void onTextChanged(CharSequence s, int start, int before, int count) {
  32.  
  33. }
  34.  
  35. @Override
  36. public void afterTextChanged(Editable s) {
  37. if(textInputEditText.getText().toString().length()>textInputLayout.getCounterMaxLength()|
  38. textInputEditText.getText().toString().length()<6){
  39. textInputLayout.setError("密码长度为6~10位");
  40. }
  41. else{
  42. textInputLayout.setError("");
  43. }
  44.  
  45. }
  46. });
  47. }
  48. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:orientation="vertical"
  7. android:gravity="center"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10.  
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="horizontal">
  15.  
  16. <TextView
  17. android:id="@+id/textView"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="用户名" />
  21.  
  22. <EditText
  23. android:id="@+id/editText"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:ems="10"
  27. android:inputType="textPersonName"
  28. android:text="" />
  29. </LinearLayout>
  30.  
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="100dp"
  34. android:orientation="horizontal">
  35.  
  36. <TextView
  37. android:id="@+id/textView2"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="密码" />
  41.  
  42. <android.support.design.widget.TextInputLayout
  43. android:id="@+id/textinputlayout1"
  44. android:layout_width="match_parent"
  45. android:layout_height="match_parent"
  46. app:counterMaxLength="10">
  47.  
  48. <android.support.design.widget.TextInputEditText
  49. android:id="@+id/textinputedit1"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:hint="请输入密码"
  53. android:inputType="textPassword" />
  54. </android.support.design.widget.TextInputLayout>
  55. </LinearLayout>
  56.  
  57. <LinearLayout
  58. android:layout_width="match_parent"
  59. android:layout_height="wrap_content"
  60. android:orientation="horizontal">
  61.  
  62. <Button
  63. android:id="@+id/button"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_weight="1"
  67. android:text="登录" />
  68.  
  69. <Button
  70. android:id="@+id/button2"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:layout_weight="1"
  74. android:text="取消" />
  75. </LinearLayout>
  76.  
  77. </LinearLayout>

第三十一篇-TextInputLayout(增强文本输入)的使用的更多相关文章

  1. Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  2. Python之路(第三十一篇) 网络编程:简单的tcp套接字通信、粘包现象

    一.简单的tcp套接字通信 套接字通信的一般流程 服务端 server = socket() #创建服务器套接字 server.bind() #把地址绑定到套接字,网络地址加端口 server.lis ...

  3. 【转】Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  4. Python之路【第三十一篇】:django ajax

    Ajax 文件夹为Ajaxdemo 向服务器发送请求的途径: 1.浏览器地址栏,默认get请求: 2.form表单: get请求 post请求 3.a标签,超链接(get请求) 4.Ajax请求 特点 ...

  5. 第三十一篇、iOS 9版本适配

    1.网络适配(强制回退HTTP) 为了强制增强数据访问安全, iOS9 默认会把 所有的http请求 所有从NSURLConnection . CFURL . NSURLSession发出的 HTTP ...

  6. 第三十一篇 -- 理一下.h和.cpp的关系

    今天突然想到一个问题,我们平时写代码会将代码进行分类,写到不同的cpp里,然后要用到那个类里面的函数,就直接include .h文件就好了.然后今天就在想,.h里面都是一些声明,它是怎么链接到.cpp ...

  7. 第三十一篇:SOUI布局之相对于特定兄弟窗口

    SOUI中通过pos的标志如:[, {, }, ],这4个标志可以相对于前一个及后一个兄弟窗口,但是有时候希望相对于不是前后窗口的兄弟窗口,比如一个通过一个中心窗口同时定义它的上下左右4个窗口,这个时 ...

  8. 第三十三篇、富文本 NSMutableAttributedString

    // 设置颜色等 NSMutableDictionary *arrDic = [NSMutableDictionary dictionary]; arrDic[NSForegroundColorAtt ...

  9. 第三十一篇 玩转数据结构——并查集(Union Find)

    1.. 并查集的应用场景 查看"网络"中节点的连接状态,这里的网络是广义上的网络 数学中的集合类的实现   2.. 并查集所支持的操作 对于一组数据,并查集主要支持两种操作:合并两 ...

随机推荐

  1. ubunto启动chrome报错

    /usr/bin/google-chrome-stable[5199:5199:0703/143543.136117:ERROR:zygote_host_impl_linux.cc(88)] Runn ...

  2. python学习笔记(11)--测验3: Python基础语法(下) (第7周)

    斐波那契数列计算 B 描述 斐波那契数列如下: F(0) = 0, F(1) = 1 F(n) = F(n-1) + F(n-2) 编写一个计算斐波那契数列的函数,采用递归方式,输出不超过n的所有斐波 ...

  3. Python——Entry、Text控件

    background(bg)  : 文本框背景色:  foreground(fg)    :    前景色: selectbackground :  选定文本背景色:  selectforegroun ...

  4. C#,单元测试

    C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...

  5. 2.23日刷数论题后总结(题目整理自SCUT

    第一道:Rightmost digit 求N^N次最后一个数字 快速幂mod10咯 代码如下: #include <cstdio> #define ll long long using n ...

  6. ubuntu6.04安装

    一.在windows操作系统下准备ubuntu系统的安装盘 1. 下载ubuntu的ISO文件 这一步相对简单,网络上面有很多的链接下载.这里贴一个ubuntu的官方网站链接,可以下载到ubuntu ...

  7. mpvue——Error: EPERM: operation not permitted

    报错 $ npm run build > mpvue@ build D:\wamp\www\webpack\mpvue\my-project > node build/build.js w ...

  8. 洛谷p1586四方定理题解

    题目 这个题的本质是动态规划中的背包问题. 为什么会想到背包呢. 因为往往方案数不是排列组合就是递推或者是dp,当然还有其他的可能.我们可以把一个数的代价当成这个数的平方,价值就是一个方案数.由于这个 ...

  9. 为何在新线程中使用注解获取不到Spring管理的Bean

    新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...

  10. jQuery 方式模拟提交表单

    //add test moudle define(function(require , exports , module) { //=========== 不使用模块化只使用如下代码即可 start ...