android输入法(2)–InputMethodManager
在个别时候,需要强制隐藏Android输入法键盘,如当前键盘正在显示,这个时候点击了侧滑面板,就要强制隐藏输入法键盘。网上常见的方法有:
1、 InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
这个是让输入法状态发生逆转,如果当前未显示则显示出来。如果显示出来,则隐藏。因此是不符合本文所说的应用场景的。
2、imm.hideSoftInputFromInputMethod(passwdEdit.getWindowToken(), 0);
这个经本人在android4.2机子上测试无效。
3、imm.hideSoftInputFromInputMethod(getActivity().getCurrentFocus().getWindowToken(), 0);
经本人测试,依然无效。大爷的,为了这个小功能浪费了杂家两个小时时间。
4、imm.hideSoftInputFromWindow(passwdEdit.getWindowToken(), 0);
经过测试,这是唯一有效的方法!
另外,有人说:
InputMethodManager imm = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
Log.i(TAG, "imm.isActive() = " + isOpen);
用以上代码来判断当前输入法键盘是否弹出可见,完全扯淡,isOpen一直为true。特此记录。
典型应用
1.调用显示系统默认的输入法
方法一、
1 |
2 |
3 |
4 |
5 |
m_receiverView(接受软键盘输入的视图(View)
InputMethodManager.SHOW_FORCED(提供当前操作的标记,SHOW_FORCED表示强制显示)
方法二、
1 |
2 |
3 |
这个InputMethodManager类里面的toggleSoftInput方法的API中的解释是:
This method toggles the input method window display. If the input window is already displayed, it gets hidden. If not the input window will be displayed.
这个方法在界面上切换输入法的功能,如果输入法出于现实状态,就将他隐藏,如果处于隐藏状态,就显示输入法
2.调用隐藏系统默认的输入法
1 |
2 |
3 |
(WidgetSearchActivity是当前的Activity)
3.获取输入法打开的状态
1 |
2 |
3 |
isOpen若返回true,则表示输入法打开