EditText mEditText;
mEditText.addTextChangedListener(TextWatcher watcher); //添加文字變化監聽
TextWatcher
- afterTextChanged(Editable s); 其中s.toString()得到變化后的文字,s.length()得到文字的長度;
設置鍵盤按鈕類型并監聽
注意:一定要設置屬性:android:inputType="text",否則不起作用
xml里設置:
android:imeOptions="actionSearch"
代碼里設置:
mEditText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
事件監聽:
mEditText.setOnEditorActionListener(new EditText.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//TODO 搜索處理事件
}
return false;
}
} );
焦點事件
一個頁面若有EditText,則該EditText獲取默認的焦點,若取消則在父控件上添加屬性:
android:focusable="true"
android:focusableInTouchMode="true"
獲取失去焦點:
mEditText.requestFocus();//獲取焦點 光標出現
mEditText.clearFocus(); //失去焦點