監聽、控制軟鍵盤的彈出和收起
阿新 • • 發佈:2019-01-08
獲取軟鍵盤的開啟狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法開啟
- 如果輸入法在視窗上已經顯示,則隱藏,反之則顯示
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (0, InputMethodManager.HIDE_NOT_ALWAYS);
2.view為接受軟鍵盤輸入的檢視,SHOW_FORCED表示強制顯示
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //強制隱藏鍵盤
3.呼叫隱藏系統預設的輸入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (MainActivity是當前的Activity)