android隐藏关闭软键盘

来源:互联网 发布:保留小数位数的函数sql 编辑:程序博客网 时间:2024/06/08 11:44

现在有一个需求是说希望可以自动的隐藏和打开软键盘,场景是使用在alert dialog中有一个文本框,弹出dialog同时自动弹出键盘,编辑后点击完成直接关闭。使用的方式是:

inputManager.showSoftInput(tvEditor,  InputMethodManager.SHOW_FORCED);

如果使用参数InputMethodManager.SHOW_IMPLICIT,就不会自动弹出键盘,但是使用SHOW_FORCED后使用各种方法都无法自动关闭键盘,必须按back键。

在vivo中很多自动隐藏键盘方法都ok,但是三星没有一个方法可行,包含不仅限于如下方式:

(1)inputManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_IMPLICIT_ONLY);

(2)dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

(3)dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

(4)inputManager.hideSoftInputFromInputMethod(tvEditor.getWindowToken(),0);

(5)inputManager.hideSoftInputFromInputMethod(dialog.getWindow().getDecorView().getWindowToken(),0);

最后无意中google到,只要toggle成对使用就ok,然后显示使用

inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

隐藏也使用

inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

目前测试三星和vivo都ok。

原创粉丝点击