EditText 在开发中经常用到得一个控件,是一些用户交互的场景中的重要组件。例如,用户的登录操作,需要用户输入账号和密码,然后我们获取用户输入的数据,把数据提交给服务器进行验证。下面整理了EditText控件的常用属性和一些用法,仅供参考。

1. 基本属性
android:autoLink//设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)。
android:autoText//如果设置,将自动执行输入值的拼写纠正。
android:bufferType//指定getText()方式取得的文本类别。
android:capitalize//设置英文字母大写类型。
android:cursorVisible//设定光标为显示/隐藏,默认显示。
android:digits//设置允许输入哪些字符。如“1234567890.+-*/% ()”
android:drawableBottom//在text的下方输出一个drawable。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
android:drawableLeft//在text的左边输出一个drawable。
android:drawablePadding//设置text与drawable的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
android:drawableRight//在text的右边输出一个drawable。
android:drawableTop//在text的正上方输出一个drawable。
android:editable//设置是否可编辑。
android:editorExtras//设置文本的额外的输入数据。
android:ellipsize//设置当文字过长时,该控件该如何显示。有如下值设置:“start”—?省略号显示在开头;“end”——省略号显示在结尾;“middle”—-省略号显示在中间;“marquee” ——以跑马灯的方式显示(动画横向移动)
android:freezesText//设置保存文本的内容以及光标的位置。
android:gravity//设置文本位置,如设置成“center”,文本将居中显示。
android:hintText//为空时显示的文字提示信息。
android:textColorHint//设置提示信息的颜色。
android:imeOptions//附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。
android:imeActionId//设置IME动作ID。
android:imeActionLabel//设置IME动作标签。
android:includeFontPadding//设置文本是否包含顶部和底部额外空白,默认为true。
android:inputMethod//为文本指定输入法,需要完全限定名(完整的包名)。
android:inputType//设置文本的类型,用于帮助输入法显示合适的键盘类型。
android:linksClickable//设置链接是否点击连接,即使设置了autoLink。
android:marqueeRepeatLimit//在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
android:ems//设置TextView的宽度为N个字符的宽度。
android:maxEms//设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:minEms//设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:maxLength//限制显示的文本长度,超出部分不显示。
android:lines//设置文本的行数,设置两行就显示两行,即使第二行没有数据。
android:maxLines//设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。
android:minLines//设置文本的最小行数,与lines类似。
android:lineSpacingExtra//设置行间距。
android:lineSpacingMultiplier//设置行间距的倍数。如“1.2”
android:numeric//如果设置“true”,输入法自动变为数字输入键盘,同时仅允许0-9的数字输入。
android:password//以小点“.”显示文本
android:phoneNumber//设置为电话号码的输入方式。
android:privateImeOptions//设置输入法选项。
android:scrollHorizontally//设置文本超出TextView的宽度的情况下,是否出现横拉条。
android:selectAllOnFocus//如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。
android:shadowColor//指定文本阴影的颜色,需要与shadowRadius一起使用。
android:shadowDx//设置阴影横向坐标开始位置。
android:shadowDy//设置阴影纵向坐标开始位置。
android:shadowRadius//设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
android:singleLine//设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。
android:textStyle//设置字形;bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2;可以设置一个或多个,用“|”隔开。
android:typeface//设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3
android:maxHeight//设置文本区域的最大高度。
android:minHeight//设置文本区域的最小高度。
android:maxWidth//设置文本区域的最大宽度。
android:minWidth//设置文本区域的最小宽度。
android:textAppearance//设置文字外观。如"?android:attr/textAppearanceLargeInverse" 这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。
2. 限制输入内容
通过虚拟键盘来控制输入框中内容的类型,可以在xml文件中加入android:inputType="";
inputType 有下面这些值:
date //日期键盘
datetime //日期时间格式
none //无格式
number //数字格式
numberDecimal //可以带小数点的浮点格式
numberPassword //数字密码格式
numberSigned //有符号数字格式
phone //拨号键盘
text //文本格式
textAutoComplete //自动完成
textAutoCorrect //纠正单词的拼写错误
textCapCharacters //所有字符大写
textCapSentences //仅第一个字母大写
textCapWords //单词首字母大写
textEmailAddress //电子邮件地址格式
textEmailSubject //邮件主题格式
textFilter //文本筛选格式
textImeMultiLine //输入法多行
textLongMessage //长消息格式
textMultiLine //多行输入
textNoSuggestions //不提示
textPassword //密码格式
textPersonName //人名格式
textPhonetic //拼音输入格式
textPostalAddress //邮政格式
textShortMessage //短消息格式
textUri //URI格式
textVisiblePassword //密码可见格式
textWebEditText //作为网页表单的文本格式
textWebEmailAddress //作为网页表单的电子邮件地址格式
textWebPassword //作为网页表单的密码格式
time //时间键盘
3. 改变输入法中回车按钮的显示内容
根据编辑框输入完成后要执行的业务逻辑指定软键盘右下角Action按钮的样式和行为,可以在xml文件中加入android:imeOptions="";
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
。
4. 焦点处理
- 焦点事件监听
et_text.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 此处为得到焦点时的处理内容
} else {
// 此处为失去焦点时的处理内容
}
}
});
- 获取失去焦点
et.setFocusable(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
et.clearFocus();//失去焦点
et.requestFocus();//获取焦点
5. 默认不弹出软键盘
① 在 AndroidMainfest.xml中设置windowSoftInputMode属性
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustUnspecified|stateHidden"/>
② 在OnCreate()函数中,加上下面的代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
③ 让 EditText失去焦点
editText.clearFocus();
6. 设置默认输入法
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文键盘
editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文键盘
editText.setInputType(InputType.TYPE_CLASS_NUMBER);//数字键盘
7. 强制显示隐藏软键盘
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隐藏软键盘
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //显示软键盘
8. 始终不弹出软键盘
① 在XML文件中,Edittext父布局上进行如下设置:
android:focusable="true"
android:focusableInTouchMode="true"
② 在Java代码中,添加下面属性:
editText.setInputType(InputType.TYPE_NULL);
9. 显示隐藏密码
① 在XML文件中设置:
Android:password="true" // 以”.”形式显示密码文本
android:inputType="textPassword" //不可见密码
android:inputType="textVisiblePassword" //可见密码
② 在Java代码中设置:
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
10. 输入状态监听
et_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//s 输入框中改变前的字符串信息
//start 输入框中改变前的字符串的起始位置
//count 输入框中改变前后的字符串改变数量一般为0
//after 输入框中改变后的字符串与起始位置的偏移量
Log.d("TAG","beforeTextChanged--------------->");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//s 输入框中改变后的字符串信息
//start 输入框中改变后的字符串的起始位置
//before 输入框中改变前的字符串的位置 默认为0
//count 输入框中改变后的一共输入字符串的数量
Log.d("TAG","onTextChanged--------------->");
}
@Override
public void afterTextChanged(Editable s) {
//s 输入结束呈现在输入框中的信息
Log.d("TAG","afterTextChanged--------------->");
}
});
11. 光标设置
- 指定光标位置
EditText et = (EditText) findViewById(R.id.editText);
et.setSelection(et.getText().toString().length());
-
光标显示隐藏
xml布局:
android:cursorVisible="true"//显示
android:cursorVisible="false"//隐藏
Java代码:
edit.setCursorVisible(true);//显示
edit.setCursorVisible(false);//隐藏
-
设置光标样式
1)在drawable
中创建et_cursor.xml
文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#666666" />
<size android:width="1dp" />
</shape>
2)在Edittext
中引用et_cursor.xml
文件
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:textCursorDrawable="@drawable/et_cursor"/>
网友评论