軟鍵盤的關閉和顯示

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章