软键盘的关闭和显示

来源:互联网 发布:台北北投温泉攻略 知乎 编辑:程序博客网 时间:2024/04/26 08:59

this.getCurrentFocus() 

Boolean isOpen=imm.isActive(); 这个返回true;

最后发现是

inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),

InputMethodManager.HIDE_NOT_ALWAYS);

中OpeListActivity.this.getCurrentFocus()  这个为空,所以报错。

 没想到其他解决方法。就try catch;,,至少不报错了。

一、打开输入法窗口:

<span style="font-size:14px;">InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);// 接受软键盘输入的编辑文本或其它视图</span><pre name="code" class="java" style="color: rgb(51, 51, 51); line-height: 26px;"><span style="font-size:14px;">inputMethodManager<span style="font-family: Arial, Helvetica, sans-serif;">.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);</span></span>

二、关闭出入法窗口

<span style="font-size:14px;">InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);</span>
<span style="font-family: 'Microsoft YaHei'; color: rgb(51, 51, 51); line-height: 26px; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">//接受软键盘输入的编辑文本或其它视图</span></span>
<span style="color: rgb(51, 51, 51); line-height: 26px; font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">inputMethodManager.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);</span></span>

三、如果输入法打开则关闭,如果没打开则打开

<span style="font-size:14px;">InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);</span>

四、获取输入法打开的状态

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);boolean isOpen=imm.isActive();

isOpen若返回true,则表示输入法打开


 软键盘显示的原理软件盘的本质是什么?软键盘其实是一个Dialog!

 InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统对活动主窗口进行调整,从而为输入法腾出相应的空间,然后将该Dialog显示在底部,或者全屏显示。


 转自:http://blog.csdn.net/zgf1991/article/details/7754972

0 0