Android Design Support Library(2)- TextInputLayout的使用
原创文章,转载请注明 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的使用的更多相关文章
- Android学习之Design Support Library中TextInputLayout的使用
今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也非常easy,故此给大 ...
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- 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 ...
- Material Design 开发利器:Android Design Support Library 介绍
转自:https://blog.leancloud.cn/3306/ Android 5.0 Lollipop 是迄今为止最重大的一次发布,很大程度上是因为 material design —— 这是 ...
- Android Design Support Library——Navigation View
前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...
- 如何使用android design support library
Android应用Design Support Library完全使用实例 - OPEN 开发经验库http://www.open-open.com/lib/view/open143338585611 ...
- Android Design Support Library 中控件的使用简单介绍(一)
Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...
随机推荐
- 查看端口被占用并结束进程 mac
- Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化
D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #315 (Div. 2) A 水且坑
A. Music time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 短文对话的神经反应机 -- Neural Responding Machine for Short-Text Conversation学习笔记
最近学习了一篇ACL会议上的文章,讲的是做一个短文对话的神经反映机, 原文: 会议:ACL(2015) 文章条目: Lifeng Shang, Zhengdong Lu, Hang Li: Ne ...
- Xposed初体验
Xposed初体验 1 测试环境 硬件:小米2s 16GB 电信版 系统:MIUI 4.4.18(开发版) Xposed版本: 2.5 注:Xposed版本号必须大于2.3,MIUI系统版本号也必须大 ...
- 在npm当中发布自己的包的方法
首先需要一个注册一个npm账号,注意,必须验证邮箱,不然是无法发布包的!下面是当时的报错 接着在你需要发布的包的文件夹下面打开你的cmd或者其他的命令行输入工具 输入 npm init 初始化你的 ...
- 【13】vuex2.0 之 state
Vuex 的核心是 store, 它是一个通过 Vuex.Store 构造函数生成的对象.为什么它会是核心呢?因为我们调用这个构造函数创建store 对象的时候,给它传递参数中包装了state, mu ...
- iOS不用官方SDK实现微信和支付宝支付XHPayKit
作者:朱晓辉Allen 链接:https://juejin.im/post/5a90dd3a6fb9a0634912b755 前言 前段时间由于项目需求,移除了项目中的微信支付SDK和支付宝支付SDK ...
- Nature Reserve
Nature Reserve time limit per test:2 seconds memory limit per test:256 megabytes input:standard inpu ...
- Python 复习-1
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/10/27 22:46 # @Author : lijunjiang # @Fi ...