1. 程式人生 > >Edittext不自動獲取焦點解決方法以及獲取焦點後的事件實現方法

Edittext不自動獲取焦點解決方法以及獲取焦點後的事件實現方法

解決之道:在EditText的父級控制元件中找一個,設定成
                        android:focusable="true"  
                        android:focusableInTouchMode="true"

這樣,就把EditText預設的行為截斷了!

      遇到需要獲取焦點之後做一些事情、實現方法也很簡單、那就是繫結OnFocusChangeListener事件、實現onFocusChange(View v, boolean hasFocus) 方法、第二個引數就是判斷得到焦點或失去焦點、從而實現我得想要的效果、程式碼如下:


EditText searchView = (EditText) findViewById(R.id.search_text);
searchView.setOnFocusChangeListener(new android.view.View.
OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 此處為得到焦點時的處理內容
} else {
// 此處為失去焦點時的處理內容
}
}
});