EditText 简介

•简介

  EditText是一个非常重要的组件,可以说它是用户和Android应用进行数据传输窗户;

  有了它就等于有了一扇和Android应用传输的门,通过它用户可以把数据传给Android应用,然后得到我们想要的数据。

  EditText是TextView的子类,所以TextView的方法和特性同样存在于EditText中


EditText常用属性

•maxLength

  有时候我们有一些特属的需要,要求只能在EditText中输入特定个数的字符,比如身份证号、手机号码等。

  这时候就可以通过 android:maxLength 属性来设置最大输入字符个数;

  比如 android:maxLength=“4” 就表示最多能输入 4 个字符,再多了就输入不进去了。

•Hint

  有时候我们需要说明你定义的这个EditText是做什么用的,比如让输入“用户名”,或者输入“电话号码”等;

  但是你又不想在EditText前面加一个TextView来说明这是输入“用户名”的,因为这会使用一个TextView,那么怎么办呢?

  EditText为我们提供了android:hint来设置当EditText内容为空时显示的文本;

  这个文本只在EditText为空时显示,你输入字符的时候就消失了,不影响你的EditText的文本。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp">
  6.  
  7. <EditText
  8. android:id="@+id/edit_text"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="请输入用户名"/>
  12.  
  13. </LinearLayout>

运行效果:

  

  我们还可以通过 android:textColorHint 属性设置提示文本的颜色;

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp">
  6.  
  7. <EditText
  8. android:id="@+id/edit_text"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="请输入用户名"
  12. android:textColorHint="@color/black"
  13. />
  14.  
  15. </LinearLayout>

•enabled

  设置 android:enabled="false" 可以实现不可编辑,可以获得焦点。

  这时候我们看到EditText和一个TextView差不多;

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp">
  6.  
  7. <EditText
  8. android:id="@+id/edit_text"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="请输入用户名"
  12. android:textColorHint="@color/black"
  13. android:enabled="false"
  14. />
  15.  
  16. </LinearLayout>

运行效果:

  

•password

  密码输入也是Android应用常用的功能,通过配置EditText的android:password="true"就可以实现这一密码输入功能;

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp">
  6.  
  7. <EditText
  8. android:id="@+id/edit_text"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="请输入密码"
  12. android:textColorHint="@color/black"
  13. android:password="true"
  14. />
  15.  
  16. </LinearLayout>

运行效果:

  

  设置之后,我们输入的字符就会被 “.” 这样的掩码所代替。

•phoneNumber

  手机中发短信打电话是必不可少的,所以用于专门输入电话号码的文本框也是大有用途;

  有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 );

  通过设置 android:phoneNumber="true" 就可以把EditText变成只接受电话号码输入的文本框;

•inputType

  前面我们通过指定为电话号码特定格式,然后键盘类型变成了拨号专用的键盘,这个是自动变的;

  其实我们也可以通过 android:inputType 来设置文本的类型,让输入法选择合适的软键盘;

  android:inputType有很多类型:

  • 文本类型,多为大写、小写和数字符号:

    • android:inputType="none"

    • android:inputType="text"

    • android:inputType="textCapCharacters"

    • android:inputType="textCapWords"

    • android:inputType="textCapSentences"

    • android:inputType="textAutoCorrect"

    • android:inputType="textAutoComplete"

    • android:inputType="textMultiLine"

    • android:inputType="textImeMultiLine"

    • android:inputType="textNoSuggestions"

    • android:inputType="textUri"

    • android:inputType="textEmailAddress"

    • android:inputType="textEmailSubject"

    • android:inputType="textShortMessage"

    • android:inputType="textLongMessage"

    • android:inputType="textPersonName"

    • android:inputType="textPostalAddress"

    • android:inputType="textPassword"

    • android:inputType="textVisiblePassword"

    • android:inputType="textWebEditText"

    • android:inputType="textFilter"

    • android:inputType="textPhonetic"

  • 数值类型

    • android:inputType="number"

    • android:inputType="numberSigned"

    • android:inputType="numberDecimal"

    • android:inputType="phone"...............拨号键盘

    • android:inputType="datetime"

    • android:inputType="date"..................日期键盘

    • android:inputType="time"..................时间键盘

•设置最小行,最多行,单行,多行,自动换行

  • android:minLines="3" : 设置最小行的行数

  • android:maxLines="3" : 设置EditText最大的行数

    • 当输入内容超过 maxline , 文字会自动向上滚动

  • android:singleLine="true" : 限制EditText只允许单行输入,而且不会滚动

•设置文字间隔,设置英文字母大写类型

  • android:textScaleX="1.5" : 设置字与字的水平间隔

  • android:textScaleY="1.5" : 设置字与字的垂直间隔

  • android:capitalize=" " : 默认none,提供了三个可选值

    • sentences : 仅第一个字母大写

    • words : 每一个单词首字母大小,用空格区分单词

    • characters : 每一个英文字母都大写

Android Studio 之 EditText的更多相关文章

  1. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  2. android studio 1.0 开发 ndk 调用 c++ so库

    一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟 ...

  3. android studio 各种问题

    1.dexDebug ExecException finished with non-zero exit value 2 全bug日志如下: (Error:Execution failed for t ...

  4. [App]Android Studio First App

    准备着看Android Studio的体验如何. 通过Android Studio构建一个默认的项目,添加一些元素 <RelativeLayout xmlns:android="htt ...

  5. 在Android Studio中进行单元测试和UI测试

    本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...

  6. android studio Activity标题栏研究

    第一次研究时间:2016/7/30,以下研究主要存在于当前最新版本的android studio上.eclipse请参考 一.头部标题取消 当前版本新建工程在 application中默认主题为 an ...

  7. 为Android Studio中的SettingsActivity定制任务栏

    Android Studio为开发者提供了很多内建的Activity, 其中Settings Activity是非常有用且功能强大的一种内建Activity. Setting Activity其实本质 ...

  8. Android Studio精彩案例(七)《ToolBar使用详解<一>》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 本文参考博客:http://blog.csdn.net/h_zhang/article/details/51232773 http:/ ...

  9. Android Studio精彩案例(五)《JSMS短信验证码功能实现》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 很多应用刚打开的时候,让我们输入手机号,通过短信验证码来登录该应用.那么,这个场景是怎么实现的呢?其实是很多开放平台提供了短信验证功能 ...

随机推荐

  1. Flutter CLI commands All In One

    Flutter CLI commands All In One Flutter run key commands. r Hot reload. R Hot restart. h Repeat this ...

  2. Android Activity All In One

    Android Activity All In One Android Activity Lifecycle https://developer.android.com/reference/andro ...

  3. learning 2018

    learning 2018 https://scotch.io/courses https://laod.cn/hosts/ipv6-dns.html xgqfrms 2012-2020 www.cn ...

  4. js & h5 & app & object storage

    js & h5 & app & object storage API https://developer.mozilla.org/en-US/docs/Web/API Stor ...

  5. 安装 Angular Material UI

    文档 调色板 安装 ng add @angular/material ? Choose a prebuilt theme name, or "custom" for a custo ...

  6. PAUL ADAMS ARCHITECT:华州加州城市楼市竞争力居全美前列

    美国房地产公司最新一份报告显示,美国楼市竞争力最高的十个城市中有八个来自华盛顿州和加利福尼亚州,主要原因在于越来越多的买家考虑迁居房价更划算的都会区.数据显示,斯潘那维65%的房屋以高于要价的价格售出 ...

  7. spring5学习笔记

    Spring5 框架概述 1.Spring 是轻量级的开源的 JavaEE 框架 2.Spring 可以解决企业应用开发的复杂性 3.Spring 有两个核心部分:IOC 和 Aop (1)IOC:控 ...

  8. Jmeter beanshell编程实例

    1.引言 BeanShell是一种小型的,免费的,可嵌入的符合Java语法规范的源代码解释器,具有对象脚本语言特性. 在Jmeter实践中,由于BeanShell组件较高的自由度,通常被用来处理较为复 ...

  9. servlet+Http

    Servlet:server applet 1.概念:运行在服务器端的小程序. *servlet就是一个接口,定义了Java类被服务器访问到(tomcat识别)的规则. *我们定义一个类,实现serv ...

  10. 【Notes_8】现代图形学入门——几何(基本表示方法、曲线与曲面)

    几何 几何表示 隐式表示 不给出点的坐标,给数学表达式 优点 可以很容易找到点与几何之间的关系 缺点 找某特定的点很难 更多的隐式表示方法 Constructive Solid Geometry .D ...