2010-11-20 176 views

回答

1

您可以嘗試在佈局的另一個元素上執行SetFocus()

如果你正在談論的「輸入/ OK /返回」鍵盤本身的按鈕,您可能必須建立在EditText控制KeyListener爲了知道什麼時候到另一個元件上SetFocus()

+0

我明白,這是很多人給出的解決方案,但我不喜歡它,因爲而不是input.clearFocus()簡單地工作(即從那個輸入中清除焦點),你現在必須把焦點放在其他東西上!?這看起來反直覺。 – marienke 2017-04-12 13:45:30

11

在佈局XML文件中,指定您的EditText的imeOption:

android:imeOptions="actionGo" 

接下來,在活動的Java文件

mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == EditorInfo.IME_ACTION_GO) { 
       // hide virtual keyboard 
       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0); 
       return true; 
      } 
      return false; 
     } 
    }); 

凡mYourEditText是一個EditText添加動作監聽到你的EditText對象

+0

恩,謝謝。但即時通訊suposed編寫imeOpt在佈局或編輯文本? 和我在EditorInfo上有錯誤,有什麼想法? – carefacerz 2010-11-22 16:11:56

+0

你能解釋一下EditorInfo嗎? – carefacerz 2010-11-22 18:54:44

+0

這將關閉鍵盤,但不會在所有情況下移除焦點。 IE瀏覽器將要求重點關注可以關注的第一個可用視圖。 – lostintranslation 2017-01-31 17:20:05

12
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0); 
+1

這隻會隱藏鍵盤。它不會消除焦點。 – lostintranslation 2017-01-31 17:20:34

2
private void hideDefaultKeyboard() { 
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
    //you have got lot of methods here 
} 
1

確保您的EditText XML有:

android:id="@+id/myEditText"  
android:imeOptions="actionDone" 

然後設置監聽到你的EditText(與科特林,並從片段):

myEditText.setOnEditorActionListener({ v, actionId, event -> 
      if (actionId == EditorInfo.IME_ACTION_DONE) { 
       myEditText.clearFocus() 
       val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager 
       imm.hideSoftInputFromWindow(view!!.windowToken, 0)  
      } 
      false 
     })