1. 程式人生 > >用Android程式開啟和關閉輸入法

用Android程式開啟和關閉輸入法

一、開啟輸入法視窗:
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受軟鍵盤輸入的編輯文字或其它檢視
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);

二、關閉出入法視窗
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken()
,InputMethodManager.HIDE_NOT_ALWAYS);

//接受軟鍵盤輸入的編輯文字或其它檢視
inputMethodManager.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);

三、如果輸入法開啟則關閉,如果沒開啟則開啟
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

四、獲取輸入法開啟的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法開啟