Android Studio 的原生输入框控件 EditText 属性配置详解

Android Studio 的原生输入框控件 EditText 属性配置详解

文本设置

android:hint="默认文本设置"
android:textColorHint="#95A1AA" // 默认文本颜色
android:textColorHighlight=“#666666” // 文字选中颜色
android:selectAllOnFocus="true" // 获取焦点后是否全选文本内容
android:textScaleX="1.5" // 设置字与字的水平间隔
android:textScaleY="1.5" // 设置字与字的垂直间隔
android:capitalize 默认none,提供了三个可选值:
	sentences:仅第一个字母大写
	words:每一个单词首字母大小,用空格区分单词
	characters:每一个英文字母都大写
android:paddingTop="5dp" // 文字与组件边框的距离

文本类型

android:inputType="none" <!--无格式-->
android:inputType="phone" <!--拨号键盘-->
android:inputType="text" <!--文本格式-->
android:inputType="textAutoComplete" <!--自动完成-->
android:inputType="textAutoCorrect" <!--纠正单词的拼写错误-->
android:inputType="textCapCharacters" <!--所有字符大写-->
android:inputType="textCapSentences" <!--仅第一个字母大写-->
android:inputType="textCapWords" <!--单词首字母大写-->
android:inputType="textEmailAddress" <!--电子邮件地址格式-->
android:inputType="textEmailSubject" <!--邮件主题格式-->
android:inputType="textFilter" <!--文本筛选格式-->
android:inputType="textImeMultiLine" <!--输入法多行-->
android:inputType="textLongMessage" <!--长消息格式-->
android:inputType="textMultiLine" <!--多行输入-->
android:inputType="textNoSuggestions" <!--不提示-->
android:inputType="textPassword" <!--密码格式-->
android:inputType="textPersonName" <!--人名格式-->
android:inputType="textPhonetic" <!--拼音输入格式-->
android:inputType="textPostalAddress" <!--邮政格式-->
android:inputType="textShortMessage" <!--短消息格式-->
android:inputType="textUri" <!--URI格式-->
android:inputType="textVisiblePassword" <!--密码可见格式-->
android:inputType="textWebEditText" <!--作为网页表单的文本格式-->
android:inputType="textWebEmailAddress" <!--作为网页表单的电子邮件地址格式-->
android:inputType="textWebPassword" <!--作为网页表单的密码格式-->

数值类型

android:inputType="numberPassword" <!--数字密码格式-->
android:inputType="numberSigned" <!--有符号数字格式-->
android:inputType="number" <!--数字格式-->
android:inputType="numberDecimal" <!--可以带小数点的浮点格式-->
android:inputType="phone" <!--拨号键盘-->
android:inputType="datetime" <!--日期-->
android:inputType="date" <!--日期-->
android:inputType="time" <!--时间-->

行数设置

android:minLines="3" // 最小行数
android:maxLines="3" // 最大行数
android:singleLine="true" // 设置只允许单行输入,而且不会滚动

改变输入法中回车按钮的显示内容

android:imeOptions="actionNone"
<!--imeOptions有下面一些常用值-->
<!--actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED;-->
<!--actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE;-->
<!--actionGo执行 “开始” ,对应常量EditorInfo.IME_ACTION_GO;-->
<!--actionSearch 执行 “搜索”,对应常量EditorInfo.IME_ACTION_SEARCH;-->
<!--actionSend执行 “发送”,对应常量EditorInfo.IME_ACTION_SEND;-->
<!--actionNext 执行 “下一个”,对应常量EditorInfo.IME_ACTION_NEXT;-->
<!--actionPrevious 执行 “上一个”,对应常量IME_ACTION_PREVIOUS;-->
<!--actionDone 执行 “完成”,对应常量EditorInfo.IME_ACTION_DONE-->

其他属性设置

android:numeric="integer" <!--设置仅仅能输入整数,假设是小数则是:decimal-->
android:password="true" <!--设置仅仅能输入密码-->
android:textColor="#ff8c00" <!--字体颜色-->
android:textStyle="bold" <!--字体 bold, italic, bolditalic-->
android:textSize="20dp" <!--设置输入文本内容字体大小-->
android:textAlign="center" <!--EditText没有这个属性,但TextView有,居中-->
android:typeface="monospace" <!--字型,normal, sans, serif, monospace (标准、无衬线字体、衬线字体、等宽字体)-->
android:background="@null" <!--背景,这里设置null,意思为透明-->
android:layout_weight="1" <!--权重,控制控件之间的地位,在控制控件显示的大小时蛮实用的-->
android:cursorVisible="true" <!--设定光标为显示/隐藏,默认显示-->
android:digits="1234567890" <!--设置允许输入哪些字符,如“1234567890.+-*/% ()”-->
android:drawableRight="@drawable/xxx" <!--在EditText的右边输出一个drawable-->
android:drawableTop="@drawable/xxx" <!--在EditText的正上方输出一个drawable-->
android:drawableBottom="@drawable/xxx" <!--在EditText的下方输出一个drawable-->
android:drawableLeft="@drawable/xxx" <!--在EditText的左边输出一个drawable-->
android:drawablePadding <!--设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果-->
android:editable="true" <!--设置是否可编辑,默认可以编辑-->
android:ellipsize="start" <!--设置当文字过长时,该控件该怎样显示,例如设置以下值:"start"省略号显示在开头,"end"省略号显示在结尾,"middle"省略号显示在中间,"marquee"以跑马灯的方式显示(动画横向移动)-->
android:gravity="center" <!--设置文本位置,如设置成"center",文本将居中显示-->

修改光标的颜色和样式

1、在 drawable 文件夹下新建一个 edit_cursor_color.xml 文件,写入 shape 标签,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="#000000" />
</shape>

2、在布局文件中引用 EditText 控件时设置:

android:textCursorDrawable="@drawable/edit_cursor_color"

为 EditText 配置边框线

1、在 drawable 中写出一个边框线:textview_border.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
        <stroke android:width="0.5dp" android:color="#e1e1e1" /><!--边框颜色-->
        <solid android:color="#f8f8f8" /><!--填充色-->
        <corners android:radius="4dp" />
    </shape>
  </item>
</layer-list>

2、在需要配置的 EditText 加入 background 属性就好了

设置 EditText 默认不聚焦

在其父布局中添加:

android:focusable="true"
android:focusableInTouchMode="true"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章