Android中EditText的使用方法---持續更新
一. 設置焦點
如果在單擊某個EditText的時候想讓其他的EditText獲得輸入的焦點,那麼可以使用下面的語句,
getCurrentFocus().setFocusable(false);
getCurrentFocus().setFocusableInTouchMode(false);
而要獲得焦點的EditText:
mSearchEdit.setFocusable(true);
mSearchEdit.setFocusableInTouchMode(true);
mSearchEdit.requestFocus();
二. 設置輸入格式過濾器
為EditText設置輸入的過濾器。InputFilter,首先要注意的是對一個EditText的對象設置setFilters(InputFilter[] filters),其參數是一個inputFilter數組,即對一個EditText你可以同時設置多個輸入過濾器。而我們平時用的時候一般設置一個過濾器就可以了,所以可以作如下使用(以輸入數字為例):
InputFilter[] mDigitsFilters = new InputFilter[1];
mDigitsFilters[0] = new DigitsKeyListener(true, true);
ps: DigitsKeyListener第一個參數是Sign,如果設置為true,表示允許在數字最左端有負號,否則只能輸入正數;第二個參數是decimal,如果設置為true,表示允許在數字中有一個小數點存在。
最後setFilters(mDigitsFilters);就OK了,
InputFilter是一個接口,其實現的子類有:
DateKeyListener, DateTimeKeyListener, DialerKeyListener, DigitsKeyListener, InputFilter.AllCaps, InputFilter.LengthFilter, LoginFilter, LoginFilter.PasswordFilterGMail, LoginFilter.UsernameFilterGMail, LoginFilter.UsernameFilterGeneric, NumberKeyListener, TimeKeyListener
DateKeyListener For entering dates in a text field.
DateTimeKeyListener For entering dates and times in the same text field.
DialerKeyListener For dialing-only text entry
DigitsKeyListener For digits-only text entry
InputFilter.AllCaps This filter will capitalize all the lower case letters that are added through edits.
InputFilter.LengthFilter This filter will constrain edits not to make the length of the text greater than the specified length.
LoginFilter Abstract class for filtering login-related text (user names and passwords)
LoginFilter.PasswordFilterGMail This filter is compatible with GMail passwords which restricts characters to the Latin-1 (ISO8859-1) char set.
LoginFilter.UsernameFilterGMail This filter rejects characters in the user name that are not compatible with GMail account creation.
LoginFilter.UsernameFilterGeneric This filter rejects characters in the user name that are not compatible with Google login.
NumberKeyListener For numeric text entry
TimeKeyListener For entering times in a text field.
根據解釋應該不難理解,根據你自己的需要選擇吧。
原文链接http://hi.baidu.com/ljlkings/blog/item/9b03b40b105848d93bc763d8.html