Android 5.0新控件——TextInputLayout
Android 5.0(M)新控件——TextInputLayout
介绍之前,先直观的看一下效果
TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于TextView的,TextInputLayout只能包裹一个子节点,类似于ScrollView。
本文以EditText举例,实现的效果如上效果图,EditText输入内容以后,hint内容移动至编辑框上方。
实现
导入依赖
因为TextInputLayout是Android 5.0以后新加的库的控件(Android Design Support Library),所以在使用前先要将Library导入到项目中来
或者在gradle下添加依赖
compile 'com.android.support:design:23.0.1'
XML
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码" />
</android.support.design.widget.TextInputLayout>
到此为止,如果你运行,会发现已经有了动画效果
使用
XML布局
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
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=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/til_username"
android:layout_width="match_parent"
app:hintTextAppearance="@style/FloatingStyle"
app:hintAnimationEnabled="false"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F00F0F"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码" />
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ok"
android:text="确定" />
</LinearLayout>
测试类
package com.example.kongqw.myapplication;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mUserName;
private EditText mPassWord;
private TextInputLayout mTextInputLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextInputLayout = (TextInputLayout) findViewById(R.id.til_username);
// mUserName = (EditText) findViewById(R.id.et_username);
mPassWord = (EditText) findViewById(R.id.et_pwd);
// 通过TextInputLayout设置hint内容,也可以通过直接设置EditText的hint属性
mTextInputLayout.setHint("用户名");
}
// 确认按钮123123
public void ok(View view) {
// 方式一:通过TextInputLayout获取到里面的子控件EditText后在获取编辑的内容
String username = mTextInputLayout.getEditText().getText().toString();
// 方式二:直接通过EditText获取到里面的编辑内容
String pwd = mPassWord.getText().toString();
Toast.makeText(this, "username = " + username + "\npwd = " + pwd, Toast.LENGTH_SHORT).show();
// 显示错误信息
mTextInputLayout.setError("错误提示信息");
}
}
那么如何修改他的样式呢,我做了如下简单的总结
修改样式
取消动画
可以通过TextInputLayout对象,执行setHintAnimationEnabled(boolean enabled)方法
// false 关闭动画 true 开启动画
mTextInputLayout.setHintAnimationEnabled(false);
或者在xml里添加hintAnimationEnabled属性设置
<!-- false 关闭动画 true 开启动画 -->
app:hintAnimationEnabled="false"
- 效果
设置hint移动到上方以后的颜色和字体大小
在XML里对应的TextInputLayout标签下添加hintTextAppearance属性
<android.support.design.widget.TextInputLayout
……
app:hintTextAppearance="@style/FloatingStyle">
<EditText
…… />
</android.support.design.widget.TextInputLayout>
然后在res->values->styles.xml下添加一个style
<style name="FloatingStyle" parent="@android:style/TextAppearance">
<!-- 字体颜色 -->
<item name="android:textColor">#AA00FF</item>
<!-- 字体大小 -->
<item name="android:textSize">20sp</item>
</style>
- 效果(#AA00FF,20sp)
设置编辑文字的颜色
这个就是设置EditText的颜色
<android.support.design.widget.TextInputLayout
……>
<EditText
……
android:textColor="#FF0000" />
</android.support.design.widget.TextInputLayout>
但是hint字体颜色,在EditText里设置就不起作用,目前我还没有找到用什么方法修改,感谢哪位大神能指点一二
设置下划线的颜色
修改res->values->styles.xml下”AppTheme”里的colorAccent属性
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
……
<item name="colorAccent">#0000FF</item>
</style>
- 效果(#0000FF)
设置错误提示的字体样式
目前还没发现怎样修改,找到方法以后再续……
Android 5.0新控件——TextInputLayout的更多相关文章
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...
- 一个Activity掌握Android5.0新控件 (转)
原文地址:http://blog.csdn.net/lavor_zl/article/details/51279386 谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常 ...
- 一个Activity掌握Android4.0新控件 (转)
原文地址:http://blog.csdn.net/lavor_zl/article/details/51261380 谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常 ...
- 【Android】Anroid5.0+新控件---酷炫标题栏的简单学习
Android5.0+推出的新控件感觉特别酷,最近想模仿大神做个看图App出来,所以先把这些新控件用熟悉了. 新控件的介绍.使用等等网上相应的文章已经特别多了,题主也没那能力去写篇详解出来,本篇随笔记 ...
- Android5.0新控件CardView的介绍和使用
CardView也是5.0的新控件,这控件其实就是一个卡片啦,当然我们自己也完全可以定义这样一个卡片,从现在的微博等社App中可以看到各式各样的自定义卡片,所以这个控件意义不是很大.suppor ...
- Android5.0新控件
谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种. 1. CardView(卡片视图) CardView顾名思义是卡片视图,它继承FrameLay ...
- Android4.0新控件
谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常用的新控件有下面5种. 1. Switch的使用 Switch顾名思义,就是开关的意思,有开和关两种状态. 当Swit ...
- Android5.0新控件RecyclerVIew的介绍和兼容使用的方法
第一部分 RecyclerVIew是一个可以替代listview和Gallery的有效空间而且在support-v7中有了低版本支持,具体使用方式还是规规矩矩的适配器加控件模式.我们先来看看官网的介绍 ...
- android design 新控件
转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...
随机推荐
- [LeetCode] Maximum Swap 最大置换
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- PHPCMS v9.6.0 wap模块 SQL注入
调试这个漏洞的时候踩了个坑,影响的版本是php5.4以后. 由于漏洞是由parse_str()函数引起的,但是这个函数在gpc开启的时候(也就是php5.4以下)会对单引号进行过滤\' . 看这里: ...
- Java:扩展后的赋值运算符(带强转功能)
扩展后的赋值运算符,即 +=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=,>>>=. 代码实例一: byte a=5; a=a+5; 此 ...
- Python3玩转儿 机器学习(2)
机器学习的基本任务 分类任务 回归任务 分类任务 手写输入数字识别 分类任务: 二分类任务 判断邮件是垃圾邮件或者不是垃圾邮件 判断发放给客户信用卡有风险或者没有风险 判断病患良性肿瘤还是恶性肿瘤 判 ...
- 教你从手机中提取system镜像制作线刷救砖包的简单方法
其实在制作刷机包的过程中,有时候没有官方或者第三方提供的救砖包(线刷),那怎么办?常规的方法有两种:(此处为常规方法,回读的方式暂不说明) 1.卡刷包转线刷包 2.dd命令导出分区镜像 ...
- [SDOI 2016]征途
Description 题库链接 将一个长度为 \(n\) 的正整数序列分为 \(m\) 段,问你这 \(m\) 段最小的方差 \(v\) 为多少.输出 \(v\times m^2\) . \(1\l ...
- [NOIp 2017]宝藏
Description 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋 ...
- 【NOIP2009】Hankson 的趣味题
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解 ...
- 12563 Jin Ge Jin Qu hao
• Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...
- inline使用
二八法则: 1.将inline限定在最小的,最频繁调用的函数上面.这会使你的调试,二进制升级变得容易,并能将潜在的代码膨胀问题最小化,提高程序运行速度可能性最大化. 2.不要仅仅因为函数模板出现在头文 ...