键盘的展开和收起主要使用到类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;
只有隐藏的级别>=展开的级别,才能将键盘隐藏掉。