鍵盤的展開和收起主要使用到類InputMethodManager:http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.htmlhtml
其大體方法以下:android
1 public void hide_keyboard_from(Context context, View view) { 2 InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); 3 inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 4 } 5 6 public void show_keyboard_from(Context context, View view) { 7 InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); 8 inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); 9 }
在展開和收起方法調用的時候,都要求傳入第二個參數。大體有如下四種:less
1)HIDE_IMPLICIT_ONLY:indicate that the soft input window should only be hidden if it was not explicitly shown by the user.ide
2)HIDE_NOT_ALWAYS:to indicate that the soft input window should normally be hidden, unless it was originally shown with SHOW_FORCED
this
3)SHOW_FORCED:indicate that the user has forced the input method open (such as by long-pressing menu) so it should not be closed until they explicitly do so.spa
4)SHOW_IMPLICIT:indicate that this is an implicit request to show the input window, not as the result of a direct request by the user.code
1)和2)是用於收起鍵盤的,3)和4)是用於展開鍵盤的。orm
若是用戶是點擊輸入框彈出的鍵盤,則調用1)是沒法隱藏的,系統此時判斷使用戶有意呼喚鍵盤的!調用2)則能夠收起鍵盤;htm
若是用戶是使用3)做爲參數顯示鍵盤的,則1)和2)都是沒法隱藏鍵盤的,此時須要用參數0,強制隱藏一切鍵盤;blog
若是用戶是使用4)做爲參數顯示鍵盤的,則1)和2)都是能夠隱藏鍵盤的。
總結起來就是:Forced > explicit > implicit。
對於隱藏鍵盤,其中1)是屬於implicit,2)是屬於explicit,0則是屬於force;
對於顯示鍵盤,則4)是屬於implicit,輸入框呼出屬於explicit,3)則是屬於force;
只有隱藏的級別>=展開的級別,才能將鍵盤隱藏掉。