【Android】键盘的展开和收起

键盘的展开和收起主要使用到类InputMethodManager:http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

其大致方法如下:

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     }

在展开和收起方法调用的时候,都要求传入第二个参数。大致有以下四种:

1)HIDE_IMPLICIT_ONLY:indicate that the soft input window should only be hidden if it was not explicitly shown by the user.

2)HIDE_NOT_ALWAYS:to indicate that the soft input window should normally be hidden, unless it was originally shown with SHOW_FORCED

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.

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.

1)和2)是用于收起键盘的,3)和4)是用于展开键盘的。

 

如果用户是点击输入框弹出的键盘,则调用1)是无法隐藏的,系统此时判断使用户有意呼唤键盘的!调用2)则可以收起键盘;

如果用户是使用3)作为参数显示键盘的,则1)和2)都是无法隐藏键盘的,此时需要用参数0,强制隐藏一切键盘;

如果用户是使用4)作为参数显示键盘的,则1)和2)都是可以隐藏键盘的。

 

总结起来就是:Forced > explicit > implicit。

对于隐藏键盘,其中1)是属于implicit,2)是属于explicit,0则是属于force;

对于显示键盘,则4)是属于implicit,输入框呼出属于explicit,3)则是属于force;

只有隐藏的级别>=展开的级别,才能将键盘隐藏掉。

 

posted @ 2015-01-02 19:06  大脚印  阅读(5730)  评论(0编辑  收藏  举报