點選按鈕的時候,輸入框不獲得焦點
阿新 • • 發佈:2019-02-17
public void setFocus(View view) {
edit_layout.setFocusable(true);
edit_layout.setFocusableInTouchMode(true);
edit_layout.requestFocus();
edit_layout.requestFocusFromTouch();
}
在點選按鈕的時候,呼叫這個方法。edit_layout這個是佈局中總佈局的id.最近做一個功能,就是統計使用者在輸入框停留的時間。由於點選按鈕的時候,輸入框是沒有離開的。想了好多辦法。最後這個解決了。哈哈
<pre name="code" class="java">[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片 android:focusableInTouchMode="true" 首先看上面這句程式碼,只需要加在包裹EditText元件的父佈局裡面就可以實現取消預設得到焦點游標的操作。 [html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片 // 觸控式螢幕幕取消EditText的焦點游標 rel.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { // rel.setFocusable(true); // 如果xml檔案裡面沒設定,就需要在這裡設定 // rel.setFocusableInTouchMode(true); rel.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(et1.getWindowToken(), 0); return false; } });