EditText使用笔记

Baron Zeng bio photo By Baron Zeng

1.android:imeOptions

该属性用于修改输入法键盘里的Enter的图标或者文字,比如值为“actionSearch”,图标为搜索的图片或者文字”Search”之类的,类似的还有”Send”, “Go”等文字

2.android:inputType

该属性用于帮助输入法决定使用什么键盘,比如如果值是”textCapCharacters”时,会第一个字母大写。类似的还有”textPassword”, “textEmail”, “textPhonetic”

代码输入可以用editText.setInputType(EditorInfo.inputType);

默认是数字,但是可以输入其他,注意xml中不要设置inputType

et.setRawInputType(InputType.TYPE_CLASS_NUMBER);

3.光标显示最右边

editText.setSelection(text.length());

4.EditText不可编辑(android:editable已经过期)

<EditText 
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

代码设置

editText.setKeyListener(null);//设了就不能编辑

5.setError()

看官方demo时,发现editText有个很好的方法,setError()。可以弹出错误信息,用法如下

editText.setError(error);

6.自动换行

设置inputType会导致editText不会自动换行

7.获取焦点并弹出键盘

et.requestFocus();
et.setSelection(et.getText().toString().length());
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(et, 0);

8.最大字数

android:maxLength

9.字符串过滤

InputFilter

11.不自动获取焦点

EditText的父Layout中,加入下面的两个属性即可

android:focusable="true" 
android:focusableInTouchMode="true