Android 5.0(M)新控件——TextInputLayout

介绍之前,先直观的看一下效果

TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于TextView的,TextInputLayout只能包裹一个子节点,类似于ScrollView。

本文以EditText举例,实现的效果如上效果图,EditText输入内容以后,hint内容移动至编辑框上方。

实现

导入依赖

因为TextInputLayout是Android 5.0以后新加的库的控件(Android Design Support Library),所以在使用前先要将Library导入到项目中来

或者在gradle下添加依赖

  1. compile 'com.android.support:design:23.0.1'

XML

  1. <android.support.design.widget.TextInputLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <EditText
  5. android:id="@+id/et_pwd"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:hint="密码" />
  9. </android.support.design.widget.TextInputLayout>

到此为止,如果你运行,会发现已经有了动画效果

使用

XML布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. android:orientation="vertical"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context=".MainActivity">
  13. <android.support.design.widget.TextInputLayout
  14. android:id="@+id/til_username"
  15. android:layout_width="match_parent"
  16. app:hintTextAppearance="@style/FloatingStyle"
  17. app:hintAnimationEnabled="false"
  18. android:layout_height="wrap_content">
  19. <EditText
  20. android:id="@+id/et_username"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:textColor="#F00F0F"/>
  24. </android.support.design.widget.TextInputLayout>
  25. <android.support.design.widget.TextInputLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content">
  28. <EditText
  29. android:id="@+id/et_pwd"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:hint="密码" />
  33. </android.support.design.widget.TextInputLayout>
  34. <Button
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:onClick="ok"
  38. android:text="确定" />
  39. </LinearLayout>

测试类

  1. package com.example.kongqw.myapplication;
  2. import android.support.design.widget.TextInputLayout;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8. public class MainActivity extends AppCompatActivity {
  9. private EditText mUserName;
  10. private EditText mPassWord;
  11. private TextInputLayout mTextInputLayout;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. mTextInputLayout = (TextInputLayout) findViewById(R.id.til_username);
  17. // mUserName = (EditText) findViewById(R.id.et_username);
  18. mPassWord = (EditText) findViewById(R.id.et_pwd);
  19. // 通过TextInputLayout设置hint内容,也可以通过直接设置EditText的hint属性
  20. mTextInputLayout.setHint("用户名");
  21. }
  22. // 确认按钮123123
  23. public void ok(View view) {
  24. // 方式一:通过TextInputLayout获取到里面的子控件EditText后在获取编辑的内容
  25. String username = mTextInputLayout.getEditText().getText().toString();
  26. // 方式二:直接通过EditText获取到里面的编辑内容
  27. String pwd = mPassWord.getText().toString();
  28. Toast.makeText(this, "username = " + username + "\npwd = " + pwd, Toast.LENGTH_SHORT).show();
  29. // 显示错误信息
  30. mTextInputLayout.setError("错误提示信息");
  31. }
  32. }

那么如何修改他的样式呢,我做了如下简单的总结

修改样式

取消动画

可以通过TextInputLayout对象,执行setHintAnimationEnabled(boolean enabled)方法

  1. // false 关闭动画 true 开启动画
  2. mTextInputLayout.setHintAnimationEnabled(false);

或者在xml里添加hintAnimationEnabled属性设置

  1. <!-- false 关闭动画 true 开启动画 -->
  2. app:hintAnimationEnabled="false"
  • 效果

设置hint移动到上方以后的颜色和字体大小

在XML里对应的TextInputLayout标签下添加hintTextAppearance属性

  1. <android.support.design.widget.TextInputLayout
  2. ……
  3. app:hintTextAppearance="@style/FloatingStyle">
  4. <EditText
  5. …… />
  6. </android.support.design.widget.TextInputLayout>

然后在res->values->styles.xml下添加一个style

  1. <style name="FloatingStyle" parent="@android:style/TextAppearance">
  2. <!-- 字体颜色 -->
  3. <item name="android:textColor">#AA00FF</item>
  4. <!-- 字体大小 -->
  5. <item name="android:textSize">20sp</item>
  6. </style>
  • 效果(#AA00FF,20sp)

设置编辑文字的颜色

这个就是设置EditText的颜色

  1. <android.support.design.widget.TextInputLayout
  2. ……>
  3. <EditText
  4. ……
  5. android:textColor="#FF0000" />
  6. </android.support.design.widget.TextInputLayout>

但是hint字体颜色,在EditText里设置就不起作用,目前我还没有找到用什么方法修改,感谢哪位大神能指点一二

设置下划线的颜色

修改res->values->styles.xml下”AppTheme”里的colorAccent属性

  1. <!-- Base application theme. -->
  2. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  3. <!-- Customize your theme here. -->
  4. ……
  5. <item name="colorAccent">#0000FF</item>
  6. </style>
  • 效果(#0000FF)

设置错误提示的字体样式

目前还没发现怎样修改,找到方法以后再续……

Android 5.0新控件——TextInputLayout的更多相关文章

  1. Android 5.0新控件——FloatingActionButton(悬浮按钮)

    Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...

  2. 一个Activity掌握Android5.0新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51279386 谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常 ...

  3. 一个Activity掌握Android4.0新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51261380 谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常 ...

  4. 【Android】Anroid5.0+新控件---酷炫标题栏的简单学习

    Android5.0+推出的新控件感觉特别酷,最近想模仿大神做个看图App出来,所以先把这些新控件用熟悉了. 新控件的介绍.使用等等网上相应的文章已经特别多了,题主也没那能力去写篇详解出来,本篇随笔记 ...

  5. Android5.0新控件CardView的介绍和使用

       CardView也是5.0的新控件,这控件其实就是一个卡片啦,当然我们自己也完全可以定义这样一个卡片,从现在的微博等社App中可以看到各式各样的自定义卡片,所以这个控件意义不是很大.suppor ...

  6. Android5.0新控件

    谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种.  1. CardView(卡片视图) CardView顾名思义是卡片视图,它继承FrameLay ...

  7. Android4.0新控件

    谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常用的新控件有下面5种.  1. Switch的使用 Switch顾名思义,就是开关的意思,有开和关两种状态. 当Swit ...

  8. Android5.0新控件RecyclerVIew的介绍和兼容使用的方法

    第一部分 RecyclerVIew是一个可以替代listview和Gallery的有效空间而且在support-v7中有了低版本支持,具体使用方式还是规规矩矩的适配器加控件模式.我们先来看看官网的介绍 ...

  9. android design 新控件

    转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...

随机推荐

  1. Spring-cloud(五) 使用Ribbon进行Restful请求

    写在前面 本文由markdown格式写成,为本人第一次这么写,排版可能会有点乱,还望各位海涵. 主要写的是使用Ribbon进行Restful请求,测试各个方法的使用,代码冗余较高,比较适合初学者,介意 ...

  2. java开发----自定义对象,重写equals方法

    javaweb开发中,用到了好多自定义对象,这时候如果不重写equals方法,很多时候都会返回false, 因此我们必须习惯重写这个方法. 重点: 1.equals比较俩对象时比较的是对象引用是否指向 ...

  3. codevs 1069 关押罪犯

    提交地址:http://codevs.cn/problem/1069/ 1069 关押罪犯 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻 ...

  4. ABP领域层知识回顾之---实体

    标题:重温ABP领域层 1. 前言  最近一段时间一直在看<ABP的开发指南>(基于DDD的经典分层架构思想).因为之前一段时间刚看完<领域驱动设计:软件核心复杂性应对之道>, ...

  5. [ZJOI2009]染色游戏

    Description 一共n × m 个硬币,摆成n × m 的长方形.dongdong 和xixi 玩一个游戏, 每次可以选择一个连通块,并把其中的硬币全部翻转,但是需要满足存在一个 硬币属于这个 ...

  6. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  7. AR934X built-in switch链路检测问题及处理方法

    1 问题 在使用QSDK平台配合QCA9531方案时,碰到过2个实在无解的问题,其一:将有线口连接到其它傻瓜交换机上,然后通过无线或另一个有线口登录的设备上,执行ifconfig ethx down, ...

  8. Spring源码分析(一)--BeanProcessor

    一.何谓BeanProcessor BeanProcessor是SpringFramework里非常重要的核心接口之一,我先贴出一段源代码: /* * Copyright 2002-2015 the ...

  9. mac电脑操作

    1.在mac电脑上打开多个终端: command+n快捷键可以打开多个终端  

  10. cocos2d-x-3.x 学习总结(一)

    这周学习了<cocos2d-x 3.x 游戏开发之旅>的第三章,做如下总结: 1.关于创建标签对象 书中是 Label* label = Label::create(); 可是总是提示出错 ...