频道栏目
首页 > 资讯 > Android > 正文

Android中EditText的inputType属性的详解

17-04-24        来源:[db:作者]  
收藏   我要投稿

Android中EditText的inputType属性的详解。上个星期看公司一个项目中灵活用到edittext 属性inputtype。inputtype属性不仅可以再xml里面定义。还可以在activity设置的。项目应用场景是这样。一个页面需要很多的对话框输入,包括需要字符型,数字型,或者密码输入。但是只做了一个弹出对话框,然后不同调用这个对话框,用唯一码识别那个按键调用弹出。那么弹出来对话框(输入类型)要灵活设置。就需要activity传递inputtype这个属性值进行设置。因为这个对话框都只调用了一个xml。这是一个场景吧。

一、先说说xml里面的调用,这个都不陌生的说。

[html] view plain copy

android:inputType="number"/>

这里输入是数字吧。

二、activity里的调用。或者其他class。

[java] view plain copy

EditTexttestEditText=(EditText)findViewById(R.id.edittext);intinputType=InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_VARIATION_NORMAL;

testEditText.setInputType(inputType);

这个inputType值是不会很好用啊。灵活吧!

那我贴图看一下源码中InputType属性类吧。如下图,

这上面贴就是inputype全部属性了,这些多看看,都是英文单词。

三、顺便介绍一下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:hint="请输入数字!"设置显示在空间上的提示信息

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

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

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

android:textColor = "#ff8c00" 字体颜色

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

android:textSize="20dip" 大小

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" 空间背景,这里没有,指透明

android:layout_weight="1" 权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。不知道这样理解对不对?

通过EditText的layout xml文件中的相关属性来实现:

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

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

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

android:editable="false" 设置EditText不可编辑

android:singleLine="true" 强制输入的内容在单行

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

虽然这些都是很简单,但也很常用。细节需要注意,才能做到极致。

相关TAG标签
上一篇:《HeadFirst设计模式》读书笔记-第9章-迭代器模式
下一篇:商品展示进行增删改查
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站