Android程序打开和关闭输入法
生活随笔
收集整理的這篇文章主要介紹了
Android程序打开和关闭输入法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
一、打開輸入法窗口:
/*** 動態顯示軟鍵盤** @param context 上下文* @param edit 輸入框*/ public static void showSoftInput(Context context, EditText edit) {edit.setFocusable(true);edit.setFocusableInTouchMode(true);edit.requestFocus();InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);inputManager.showSoftInput(edit, 0); }?
二、關閉出入法窗口
InputMethodManager?imm?=?(InputMethodManager)?getSystemService(Context.INPUT_METHOD_SERVICE);?? imm.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),??InputMethodManager.HIDE_NOT_ALWAYS); /*** 隱藏軟鍵盤*/ public static void hideKeyboard(Activity c) {try {InputMethodManager imm = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(c.getCurrentFocus().getWindowToken(), 0);} catch (NullPointerException e) {Log.e("hideKeyboard", e.toString());} }?
三、如果輸入法打開則關閉,如果沒打開則打開
InputMethodManager?imm?=?(InputMethodManager)?getSystemService(Context.INPUT_METHOD_SERVICE);?? imm.toggleSoftInput(0,?InputMethodManager.HIDE_NOT_ALWAYS);?
四、獲取輸入法打開的狀態
InputMethodManager?imm?=?(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);?? boolean?isOpen=imm.isActive();?? //isOpen若返回true,則表示輸入法打開?
鍵盤相關工具類:
KeyboardUtils.java
?
轉載于:https://my.oschina.net/u/2320057/blog/625842
總結
以上是生活随笔為你收集整理的Android程序打开和关闭输入法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #25
- 下一篇: iOS屏幕旋转 浅析