介绍

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. 《图解HTTP》阅读笔记-第五章-web服务器

      第五章.与HTTP协助的web服务器单台虚拟主机实现多个域名可以用单台物理主机运行多台虚拟主机,这些虚拟主机的IP相同,而虚拟主机有可以寄存多个不同主机名和域名的web网站,所以要保证发送HTTP ...

  2. 2019.2.25考试T3, 离线+线段树

    \(\color{#0066ff}{题解}\) #include<bits/stdc++.h> #define LL long long LL in() { char ch; LL x = ...

  3. [C/C++语言标准] ISO C99/ ISO C11/ ISO C++11/ ISO C++14/ISO C++17 Downloads

    语言法典,C/C++社区人手一份,技术讨(hu)论(peng)必备 ISO IEC C99 https://files.cnblogs.com/files/racaljk/ISO_C99.pdf IS ...

  4. AD属性常量类

    参考:http://www.selfadsi.org/user-attributes.htm namespace Common { /// <summary> /// AD中的属性,没有出 ...

  5. vs 部署SharePoint项目时, package丢失

    bug描述:vs部署sharepoint项目时报错:重启iis应用池失败,未将对象设置引用到实例. 解决方案:查看项目文件(包括隐藏文件),发现package文件不见了,在回收站内能找到被删除的pac ...

  6. 页面布局常用,让子级div排排坐

    画页面的时候经常遇到页面布局的问题,父级div包裹多个子div很常见,代码如下: <div> <div style='width:100px;height: 100px;backgr ...

  7. node js linux / OS 安装

    rm -rf 删除文件夹名字rm -rf 软连接名称 1.安装taryum install -y tar 3. 下载node https://nodejs.org/en/download/ 4. 拷贝 ...

  8. 停止mysql服务

    停止mysql服务windowsnet stop mysql (service mysqld stop win10不能用) linux /etc/init.d/mysqld stop

  9. springBoot学习资料

    转载:http://www.importnew.com/29363.html 官网搭建springBoot项目:https://www.cnblogs.com/lcplcpjava/p/7406253 ...

  10. Docker & ASP.NET Core 教程

    第一篇:把代码连接到容器 第二篇:定制Docker镜像 第三篇:发布镜像 第四篇:容器间的连接 第五篇: Docker & ASP.NET Core (5):Docker Compose AS ...