目录
本文介绍如何在编辑框EditText上高效地输入文本,包括:如何改变编辑框的控件外观,如何利用焦点变更监听器提前校验输入位数,如何利用文本变化监听器自动关闭软键盘。
编辑框EditText
编辑框EditText用于接收软键盘输入的文字,例如用户名、密码、评价内容等等,它由文本试图派生而来,除了TextView已有的各种属性和方法之外,EditText还支持下列XML属性。
- inputType:指定输入文本的类型。输入类型的取值说明见表,若同时使用多种文本类型,则可使用竖线(|)把多种文本类型拼接起来。
- maxLength:指定文本允许输入的最大长度。
- hint:指定提示文本的内容。
- textColorHint:指定提示文本的颜色。
输入类型 | 说 明 |
---|---|
text | 文本 |
textPassword | 文本密码。显示时用园点(·)代替 |
number | 整型数 |
numberSigned | 带符号的数字。允许在开头带负号(-) |
numberDecimal | 带小数点的数字 |
numberPassword | 数字密码。显示时用圆点(·)代替 |
datetime | 时间日期格式。除了数字外,还允许输入横线(-)、斜杠(/)、空格( )、冒号(:) |
date | 日期格式。除了数字外,还允许输入横线(-)和斜杠(/) |
time | 日期格式。除了数字外,还允许输入冒号(:) |
接下来通过XML布局观看编辑框界面效果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EditText">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:text="下面是登录信息"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="10"
android:hint="请输入用户名&#