Android 中 EditText 的 inputType 属性及其他常用属性详解

一、xml里面调用:

<---输入数字->
android:inputType="number"/> 

二、Activity 里面调用:

EditText editText=(EditText)findViewById(R.id.edittext);
intinputType=InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_VARIATION_NORMAL;
editText.setInputType(inputType);

三、xml 的 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----时间键盘

四、EditText 其他常用属性:

android:layout_gravity="center_vertical" 设置控件显示的位置:**默认top**,这里居中显示,还有bottom

android:numeric="integer" 设置只能输入整数,如果是小数则是:decimal

android:singleLine="true" 设置单行输入,一旦设置为true,则文字不会自动换行。

android:password="true" 设置只能输入密码

android:textStyle="bold" 字体,bold, italic, bolditalic

android:capitalize = "characters" 以大写字母写

android:textAlign="center" EditText没有这个属性,但TextView有 

android:textColorHighlight="#cccccc" 被选中文字的底色,默认为蓝色

android:textColorHint="#ffff00" 设置提示信息文字的颜色,默认为灰色

android:textScaleX="1.5" 控制字与字之间的间距

android:typeface="monospace" 字型,normal, sans, serif, monospace

android:background="@null" 空间背景,这里没有,指透明

五、 其他

  1. 密码框属性 android:password=”true” 这条可以让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。

  2. 纯数字 android:numeric=”true” 这条可以让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入

  3. 仅允许 android:capitalize=”cwj1987” 这样仅允许接受输入cwj1987,一般用于密码验证 下面是一些扩展的风格属性

  4. android:editable=”false” 设置EditText不可编辑

  5. android:ellipsize=”end” 自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时

猜你喜欢

转载自blog.csdn.net/hust_twj/article/details/78842693