EditText与TextView非常相似,它甚至与TextView共用了绝大部分XML属性和方法。EditText和TextView的最大区别在于:EditText可以接受用户输入。

EditText组件最重要的属性是inputType,该属性相当于HTML的<input.../>元素的type属性,用于EditText为指定类型的输入组件。inputType能接受的属性值非常丰富,而且随着Androd版本的升级,该属性能接受的类型还会增加。

EditText还派生了如下两个类。

  • AutoCmpleteTextView:带有自动完成功能的EditText。
  • ExtractEditText:它并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

   实例:用户友好的输入界面     

对于一个用户友好的输入界面而言,接受用户输入的文本框你默认会提示用户如何输入;当用户把焦点切换到输入框时,输入框自动选中其中已输入的内容,避免用户删除已有内容;当用户把焦点切换到只接受电话号码的输入框时,输入法自动切换到数字键盘。

下面程序的输入界面完成了以上功能,输入界面的界面布局如下。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写登录帐号"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"
/>
<!-- android:inputType="numberPassword"表明只能接收数字密码 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="16sp"
/>
<!-- inputType="number"表明是数值输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp"
/>
<!-- inputType="date"表明是日期输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="date"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="16sp"
/>
<!-- inputType="phone"表明是输入电话号码的输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:inputType="phone"
/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
</TableLayout>

上面的界面布局中第一个文本框通过android:hint指定了文本框的提示信息:请填写登录账号——这是该文本框默认的提示。当用户还没输入时,该文本框内默认显示这段信息;

第二个文本框通过android:inputType="numberPassword"设置这是一个密码框,而且只能接受数字密码,用户在该文本框输入的字符会以点号代替;第三个输入框通过android:inputType="number"设置为只能接受数值的输入框;第四个输入框通过android:inputType="date"指定它是一个日期输入框;第5个输入框通过android:inputType=“phone”设置为一个电话号码输入框。

使用Activity显示上面的界面布局将可以看到如图2.19所示的界面。

图2.19 友好的输入界面

EditText的功能与用法的更多相关文章

  1. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  2. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  3. 数值选择器(NumberPicker)的功能与用法

    数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值.使用该组件常用如下三个方法. setMinValue(int minVal):设置该组件支持的最小值. setMax ...

  4. 日历视图(CalendarView)组件的功能和用法

    日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...

  5. 星级评分条(RatingBar)的功能和用法

    星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似.实际上星级评分条与拖动条的用法.功能都十分接近:它们都是允许用户通过拖动条来改变进度.RatingBar与SeekBar最大区别 ...

  6. 拖动条(SeekBar)的功能和用法

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

  7. StackView的功能和用法

    StackView也是AdapterViewAnimator的子类,它也用于显示Adapter提供的系列View.SackView将会以“堆叠(Stack)”方式来显示多个列表项. 为了控制Stack ...

  8. MySQL常用存储引擎功能与用法详解

    本文实例讲述了MySQL常用存储引擎功能与用法. MySQL存储引擎主要有两大类: 1. 事务安全表:InnoDB.BDB. 2. 非事务安全表:MyISAM.MEMORY.MERGE.EXAMPLE ...

  9. MessageDigest的功能及用法(加密解密)

    MessageDigest的功能及用法 MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法.信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度 ...

随机推荐

  1. vi 编辑器跳转到指定行数

    如:跳转到25行 :set number :23

  2. hrbustoj 1125 循环小数 II(小数变分数+极限思想)

    #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #i ...

  3. Linux 互斥锁的实现原理(pthread_mutex_t)

    本文参考--http://www.bitscn.com/os/linux/201608/725217.html 和http://blog.csdn.net/jianchaolv/article/det ...

  4. 让你的MyEclipse具有jquery自动提示

    想让你的MyEclipse支持Jquery的自动提示更简单一些,照下图完成即可:      照上面图示已经完成了Jquery自动提示的配置,此时spket已经有两种AJAX库的自动提示,通过右边的De ...

  5. line-box(转)

    inline-block是什么? Inline-block是元素display属性的一个值.这个名字的由来是因为,display设置这个值的元素,兼具行内元素( inline elements)跟块级 ...

  6. 一个java 开源神经网络引擎 joone

    https://sourceforge.net/projects/joone/files/?source=navbar joone

  7. Android Guts: Intro to Loopers and Handlers

    One of the reasons I love Android API is because it contains so many useful little things. Many of t ...

  8. Cocos2d-x V2.x -- 开发进阶和高级实例教程(一) 转

    第一章 如何在多平台新建Cocos2d-x项目 yangyong2014-06-25 15:04:44848 次阅读 原文链接:   http://cn.cocos2d-x.org/tutorial/ ...

  9. SQL复习五(索引)

    SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开他的神秘面纱. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引 ...

  10. cookie讲解-------浏览器种cookie

    1  responce添加Set-Cookie参数: http request的返回responce为: [('Content-Type', 'application/x-javascript'), ...