原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50494634

这篇文章介绍下Android Design Support Library中的TextInputLayout的使用,假设你还不知道怎么使用这个Design Library请參考 http://blog.csdn.net/leejizhou/article/details/50479934,TextInputLayout使你的EditText更具有Material Design的感觉。能够便捷的把EditText的提示信息挪到上方而且能够方便的进行错误信息提示。

废话不多说。看效果 :)

控件定义
<android.support.design.widget.TextInputLayout
android:id="@+id/tl_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<EditText android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Password"
></EditText>
</android.support.design.widget.TextInputLayout>

TextInputLayout是一个父容器控件。包裹了EditText,也没什么特别的属性,使用非常easy,切记它一定是和EditText一起搭配使用的。

TextInputLayout的经常用法

tl_password.setHint("Username"); //EditText获得焦点后在上面显示的文字
tl_password.setErrorEnabled(true); //开启错误提醒
tl_password.setError("密码不能为空!"); //错误提醒的文字
tl_password.setErrorEnabled(false); //关闭错误提醒

演示效果GIF的具体源代码

Layout

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.leejz.textinputlayout.MainActivity"> <android.support.design.widget.TextInputLayout
android:id="@+id/tl_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<EditText android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
></EditText>
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:id="@+id/tl_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<EditText android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
></EditText>
</android.support.design.widget.TextInputLayout> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:text="Click"
android:id="@+id/button" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Blog:http://blog.csdn.net/leejizhou"
/> </LinearLayout>

Activity

import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
TextInputLayout tl_username;
TextInputLayout tl_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl_username=(TextInputLayout) findViewById(R.id.tl_username);
tl_username.setHint("Username");
tl_password=(TextInputLayout) findViewById(R.id.tl_password);
tl_password.setHint("Password");
//两种得到EditText对象的方法
// username=(EditText)findViewById(R.id.username);
// password=(EditText)findViewById(R.id.password);
username=tl_username.getEditText();
password=tl_password.getEditText();
//Button Click
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(password.getText().toString())){
tl_password.setErrorEnabled(true);
tl_password.setError("密码不能为空。");
}else{
tl_password.setErrorEnabled(false);
}
}
});
}
}

Ok,有疑问的地方的能够在下方留言,感谢。

Android Design Support Library(2)- TextInputLayout的使用的更多相关文章

  1. Android学习之Design Support Library中TextInputLayout的使用

    今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也非常easy,故此给大 ...

  2. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  3. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  4. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

  5. Codelab for Android Design Support Library used in I/O Rewind Bangkok session

    At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...

  6. Material Design 开发利器:Android Design Support Library 介绍

    转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...

  7. Android Design Support Library——Navigation View

    前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...

  8. 如何使用android design support library

    Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...

  9. Android Design Support Library 中控件的使用简单介绍(一)

    Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...

随机推荐

  1. Callable、Future、FutureTask_笔记

    参考:http://blog.csdn.net/javazejian/article/details/50896505 1.Callable<V>接口 Runnable接口 public ...

  2. 【bzoj4199】[Noi2015]品酒大会 后缀自动机求后缀树+树形dp

    题目描述(转自百度文库) 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒 ...

  3. hadoop自定义权限

    #1.测试类想运行hadoop的测试类,我们必须先编译hadoop mac下编译hadoop-2.7.4 然后查看测试类 org.apache.hadoop.hdfs.server.namenode. ...

  4. caffe+Ubuntu14.04.10 +cuda7.0/7.5+CuDNNv4 安装

    特别说明: Caffe 官网地址:http://caffe.berkeleyvision.org/ 本文为作者亲自实验完成,但仅限用于学术交流使用,使用本指南造成的任何不良后果由使用者自行承担,与本文 ...

  5. 在react项目当中使用redux

    如果需要在你的react项目当中使用状态管理模式的话,需要引入redux和react-redux两个插件,redux提供基本的功能,react-redux提供将redux注入react的方法. imp ...

  6. pat 团体天梯 L3-003. 社交集群

    L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...

  7. HDU 4779:Tower Defense

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  8. 【HDOJ5520】Number Link(费用流)

    题意:NxM的格子有些上面有数字,现在要把奇数跟偶数配对连起来,其他的格子连成一个个回路, 单独的相邻两个格子相连也算是一个回路按两条边算,连线不能相交, 给出相邻两个格子相连的费用,求最小的总费用, ...

  9. SQL索引基础

    原文发布时间为:2011-02-19 -- 来源于本人的百度文章 [由搬家工具导入]   一、深入浅出理解索引结构   实际上,您可以把索引理解为一种特殊的目录。微软的SQL SERVER提供了两种索 ...

  10. .net web api返回结果为json

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法 web api写api接口时默认返回的是把你的对象序列化后以 ...