原创文章,转载请注明 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的提示信息挪到上方而且能够方便的进行错误信息提示。

废话不多说。看效果 :)

  1. 控件定义
  2. <android.support.design.widget.TextInputLayout
  3. android:id="@+id/tl_password"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:layout_margin="8dp">
  7. <EditText android:id="@+id/password"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:inputType="textEmailAddress"
  11. android:hint="Password"
  12. ></EditText>
  13. </android.support.design.widget.TextInputLayout>

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

TextInputLayout的经常用法

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

演示效果GIF的具体源代码

Layout

  1. <?xml version="1.0" encoding="utf-8"?
  2. >
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.leejz.textinputlayout.MainActivity">
  9. <android.support.design.widget.TextInputLayout
  10. android:id="@+id/tl_username"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_margin="8dp">
  14. <EditText android:id="@+id/username"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:hint="Username"
  18. ></EditText>
  19. </android.support.design.widget.TextInputLayout>
  20. <android.support.design.widget.TextInputLayout
  21. android:id="@+id/tl_password"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_margin="8dp">
  25. <EditText android:id="@+id/password"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:hint="Password"
  29. ></EditText>
  30. </android.support.design.widget.TextInputLayout>
  31. <Button
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_margin="18dp"
  35. android:text="Click"
  36. android:id="@+id/button" />
  37. <TextView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_gravity="center"
  41. android:text="Blog:http://blog.csdn.net/leejizhou"
  42. />
  43. </LinearLayout>

Activity

  1. import android.support.design.widget.TextInputLayout;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. public class MainActivity extends AppCompatActivity {
  8. private EditText username;
  9. private EditText password;
  10. TextInputLayout tl_username;
  11. TextInputLayout tl_password;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. tl_username=(TextInputLayout) findViewById(R.id.tl_username);
  17. tl_username.setHint("Username");
  18. tl_password=(TextInputLayout) findViewById(R.id.tl_password);
  19. tl_password.setHint("Password");
  20. //两种得到EditText对象的方法
  21. // username=(EditText)findViewById(R.id.username);
  22. // password=(EditText)findViewById(R.id.password);
  23. username=tl_username.getEditText();
  24. password=tl_password.getEditText();
  25. //Button Click
  26. findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. if(TextUtils.isEmpty(password.getText().toString())){
  30. tl_password.setErrorEnabled(true);
  31. tl_password.setError("密码不能为空。");
  32. }else{
  33. tl_password.setErrorEnabled(false);
  34. }
  35. }
  36. });
  37. }
  38. }

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. Groovy安装

    进入Groovy的官网下载安装SDKMAN() 以下是官网的下载方法(http://www.groovy-lang.org/download.html) This tool makes install ...

  2. 【bzoj5018】[Snoi2017]英雄联盟 背包dp

    题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄,因此,他也只准 ...

  3. WMS请求GetCapabilities,变成下载mapserv.exe解决办法

    WMS1.1.1和WMS1.3.0两个版本中的几个区别: 1.WMS1.1.1中提供的DescribeLayers.GetStyles等接口在WMS1.3.0中不再提供支持,只提供GetCapabil ...

  4. pdf生成

    要用本文的方法生成PDF文件,需要两个控件:itextsharp.dll和ICSharpCode.SharpZipLib.dll,由于示例代码实在太多,我将代码全部整理出来,放在另外一个文件“示例代码 ...

  5. 小w的喜糖(candy)

    小w的喜糖(candy) 题目描述 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那会有多少种方案使得每 ...

  6. Codeforces Round #316 (Div. 2) C 思路/模拟

    C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. Python之时间:time模块

    import time   对于时间,使用最频繁的模块 1.获取当前时间 (1)时间戳 time.time() 时间戳:从1970年1月1日0点开始到现在按秒计算的偏移量 (2)时间元组 time.l ...

  8. js数据类型判断

    在一般情况下使用typeof 但是有时候typeof返回的结果都是object,比如数组和json对象的时候,这个时候需要用到 instanceof了 还有一个更好得办法,Object.prototy ...

  9. react Native 运行报错之一 gradle-2.14.1-all解压失败的问题

    在react-native run-android  时 可能出现压缩包不成功的情况,原因是压缩包失败,需要手动解压到相应目录, 目录:C:\Users\Administrator\.gradle\w ...

  10. CRB and Candies LCM 性质

    题目 CRB and Candies 题意 \[ \text{给定正整数N,求} LCM \lbrace C \left(N , 0 \right),C\left(N , 1 \right),..., ...