介绍

Layout which wraps an {@link android.widget.EditText} (or descendant) to show a floating label

when the hint is hidden due to the user inputting text.

Also supports showing an error via {@link #setErrorEnabled(boolean)} and

{@link #setError(CharSequence)}, and a character counter via

{@link #setCounterEnabled(boolean)}.

翻译:

TextInputLayout需要包裹一个EditText来实现当用户输入文本的时候,将hint作为一个浮动的标签显示的效果。使用比较多大的两个方法:

setError(CharSequence) - > 使能错误消息提示

对应属性值:app:errorEnabled=”true”

setCounterEnabled(boolean) -> 使能字符长度显示

对应属性值:app:counterEnabled=”true”


类介绍

TextInputLayout的父类是LinearLayout,源码位置

frameworks/support/design/src/android/support/design/widget/TextInputLayout.java

类结构视图

方法 意义
setTypeface 设置tf字体
getEditText 获取EditText视图
setHint 设置Hint内容
setHintEnabled 使能hint
setHintTextAppearance 设置hint的Text Style
setErrorEnabled 使能错误提示
setError 设置错误提示消息
setCounterEnabled 使能计数
setCounterMaxLength 设置输入框最大长度
setHintAnimationEnabled 使能Hint浮动动画,默认为true

总体来看,用的比较多的就是错误消息提示和计数功能,对字体的一些设置和TextView和EditText使用方法类似,这个布局的特点就是视觉感受和用户体验比简单的输入框文本框提升很多。符合google的导向,但是很多apk中很少看到人使用,应该大家有更炫酷的方法。


具体使用

布局文件

activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mraz.com.tabdemo.MainActivity"> <android.support.design.widget.TextInputLayout
android:id="@+id/til_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"> <android.support.design.widget.TextInputEditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username..." />
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:id="@+id/til_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="40"> <EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password..." />
</android.support.design.widget.TextInputLayout> <Button
android:id="@+id/bt_showerror"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="ShowError"
android:textAllCaps="false" /> <Button
android:id="@+id/bt_clearerror"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="ClearError"
android:textAllCaps="false" />
</LinearLayout>

代码内容

MainActivity.java

package mraz.com.tabdemo;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final TextInputLayout userTextInputLayout = (TextInputLayout) findViewById(R.id.til_username);
TextInputLayout passTextInputLayout = (TextInputLayout) findViewById(R.id.til_password); Button showErrorBtn = (Button) findViewById(R.id.bt_showerror);
showErrorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userTextInputLayout.setError("UserName is not correct!");
}
}); Button clearErrorBtn = (Button) findViewById(R.id.bt_clearerror);
clearErrorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userTextInputLayout.setError("");
}
}); }
}

代码上就不上注释了,如果有疑问请提出来,不过代码很简洁,应该问题不大。

实际效果



<Android 基础(十二)> TextInputLayout,让输入框更有灵性的更多相关文章

  1. Bootstrap <基础十二>下拉菜单(Dropdowns)

    Bootstrap 下拉菜单.下拉菜单是可切换的,是以列表格式显示链接的上下文菜单.这可以通过与 下拉菜单(Dropdown) JavaScript 插件 的互动来实现. 如需使用下列菜单,只需要在 ...

  2. Java基础十二--多态是成员的特点

    Java基础十二--多态是成员的特点 一.特点 1,成员变量. 编译和运行都参考等号的左边. 覆盖只发生在函数上,和变量没关系. Fu f = new Zi();System.out.println( ...

  3. Android Studio(十二):打包多个发布渠道的apk文件

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  4. <Android 基础(二十九)> Fragment (2) ~ DialogFragment

    简介 上一篇简单的介绍了下Fragment的使用方法,这一篇主要看下DialogFragment. 在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示 ...

  5. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  6. 玩转Django2.0---Django笔记建站基础十二(Django项目上线部署)

    第十二章 Django项目上线部署 目前部署Django项目有两种主流方案:Nginx+uWsGI+Django或者Apache+uWSGI+Django.Nginx作为服务器最前端,负责接收浏览器的 ...

  7. <Android 基础(二十六)> 渐变色圆角Button

    简介 总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加.作为一个Android开发者,这 ...

  8. <Android 基础(二十五)> View Animation

    简介 视图动画,主要包括位移,透明度,旋转和缩放,View本身的属性并没有发生变化,只是在这个视图上添加一些渐变的效果,所以总体而言,视图动画只能实现一些简单的动画效果,属性动画功能更强大. 使用 r ...

  9. <Android 基础(二十四)> EditText

    介绍 A text field allows the user to type text into your app. It can be either single line or multi-li ...

随机推荐

  1. java webservices 以Axis1.4方式 调用sap webservice接口.

    1. 首先需要下载Axis1.4 jar包,这个必应搜索大把,下载下来后把jar包加入eclipse工程项目路径中即可. 2. 下载mail.jar和activation.jar 俩个包.下载地址:h ...

  2. P3768 简单的数学题 杜教筛+推式子

    \(\color{#0066ff}{ 题目描述 }\) 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ij ...

  3. kuangbin专题七 HDU1166 敌兵布阵 (线段树或树状数组)

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  4. 查看详细linux系统信息的命令和方法

    查看内存大小: cat /proc/meminfo |grep MemTotaluname -a # 查看内核/操作系统/CPU信息的linux系统信息命令head -n 1 /etc/issue # ...

  5. Windows中报错:Fatal error in launcher: Unable to create process using '"' 的解决方案

    Windows 下同时存在 Python2 和 Python3 使用 pip 时系统报错:Fatal error in launcher: Unable to create process using ...

  6. react 中文文档案例四 (登陆登出按钮)

    import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Compon ...

  7. Codeforces Round #305 (Div. 2) B

    Description Mike and some bears are playing a game just for fun. Mike is the judge. All bears except ...

  8. LDAP环境搭建 OpenLDAP和phpLDAPadmin -- yum版

      前言: 前两天公司要求做一个使用LDAP和Kerberos做一个认证授权系统,然后开始学习LDAP相关知识,期间找了不少博客按照步骤来安装,可是很多博客在配置的时候,都会遇到安装过程中一两个问题卡 ...

  9. ORA-14517: Subpartition of index "string.string" is in unusable state

    今天碰到个ORA-03113, 原因不明. 猜测因为某些table DDL操作过后导致index unuable的case, 然后进行analyze table, 再碰到ORA-14517.  最后通 ...

  10. 剧本--ansible

    剧本不喜欢, 1.1 编写剧本规范:(PYyaml语法格式文件) 剧本中有层级划分 每个层级都要用两个空格进行区分 第一级标题 第二级标题 第三级标题 强调注意:一定使用ansible软件配置剧本时, ...