InputMethodManager Android主要用InputMethodManager来对软键盘进行管理。 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm 使用实例: showKeyBoard(View view) { InputMethodManager imm = (InputMethodManager) veiw.getContext().getSystemService 使用例子: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if 使用实例: InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
= null) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService( Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus ().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); } } }
= null) { InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE ); //接受软键盘输入的编辑文本或其它视图 imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED); /** 如果软键盘没打开则打开,没关闭则关闭 **/ InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); m.toggleSoftInput (0, InputMethodManager.HIDE_NOT_ALWAYS) /**获取输入法打开的状态**/ InputMethodManager imm = (InputMethodManager
imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager = null) { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService = null) { // 隐藏虚拟键盘 InputMethodManager inputmanger = (InputMethodManager) activity
imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); if (isShow) { imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); view.requestFocus (); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } /** * 隐藏软键盘 * * @param view */ public static void hideSoftKeyboard(View view) { Context context = view.getContext(); InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow
imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager = null) { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService = null) { // 隐藏虚拟键盘 InputMethodManager inputmanger = (InputMethodManager) activity
= null) { // 隐藏虚拟键盘 InputMethodManager inputmanger = (InputMethodManager) act public static void switchSoftInputMethod(Activity act) { // 方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager iMM = (InputMethodManager) act .getSystemService(Context.INPUT_METHOD_SERVICE); iMM.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }
@Override protected void onDestroy() { super.onDestroy(); mAnimator.cancel(); } 系统bug之InputMethodManager 导致内存泄漏 每次从MainActivity退出程序时总会报InputMethodManager内存泄漏,原因系统中的InputMethodManager持有当前MainActivity的引用,导致了MainActivity inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager == null) return; String[] viewArr = new String[]{"mCurRootView } } } 最后在泄漏的Activity,调用工具类 @Override protected void onDestroy() { //手动切断InputMethodManager
import android.view.ContextThemeWrapper; import android.view.View; import android.view.inputmethod.InputMethodManager activity Activity activity = activityUtils.getCurrentActivity(shouldSleepFirst); // 获取输入控制管理器服务 InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); = null) { inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0); return; } = null) { inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0); } // 如果设置了等待
输入法客户端(IMM) 代码路径:frameworks/base/core/java/android/view/inputmethod/ 主要指输入法框架的InputMethodManager, () 实例化全局调用InputMethodManager,即初始化IMM ---> InputMethodManager.java -- ensureDefaultInstanceForDefaultDisplayIfNecessary imm = new InputMethodManager(service, displayId, looper); //将PID/UID和每个IME客户端关联,然后作为跨进程服务端IPC 函数流程梳理 # 我们从InputMethodManager.java开始梳理 InputMethodManager.java -- startInput ---》 startInputInner 调用显示系统默认的输入法 方法一: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE
public void onClick(View v) { switch (v.getId()) { case R.id.traceroute_rootview: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow { View v = getCurrentFocus(); if (isShouldHideInput(v, ev)) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm !
mInstance = new LocalBroadcastManager(context.getApplicationContext()); } return mInstance; } } InputMethodManager 采用的也是给代码块加锁: static InputMethodManager sInstance; //getInstance是隐藏方法 public static InputMethodManager getInstance() { synchronized (InputMethodManager.class) { if (sInstance == null) IInputMethodManager service = IInputMethodManager.Stub.asInterface(b); sInstance = new InputMethodManager
* 隐藏软键盘 在dialog的编辑界面时 */ public static void hideSoftKeyboard(Activity activity, View view) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); }
www.open-open.com/lib/view/open1477623086381.html //此方法只是关闭软键盘 private void hintKbTwo() { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); if(imm.isActive()& =null) { imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS
在Android短视频源码开发过程中,如果对本地默认的输入法不满意,其实可以实现切换自定义输入法,通过这样的方式提升用户的体验,其实具体实现的方法很简单,用好InputMethodManager即可。 android.view.View; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager View.OnClickListener() { @Override public void onClick(View v) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //拿到输入法的全部条目
android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点 return false; } /** * 获取InputMethodManager = null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE ); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } }
et_content.setFocusable(true); et_content.setFocusableInTouchMode(true); et_content.requestFocus(); 打开软键盘 InputMethodManager inputManager =(InputMethodManager)et_content.getContext().getSystemService(Context.INPUT_METHOD_SERVICE EditorInfo.IME_ACTION_SEARCH: startSearch(); break; } return false; } 隐藏软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow
EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点 return false; } /** * 获取InputMethodManager = null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE ); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } }
结合软键盘自动隐藏在处理回车键时,如果希望在回车后隐藏软键盘,可以使用 InputMethodManager 来关闭软键盘。 // 处理回车事件 Log.d("EnterKey", "回车键被按下,隐藏键盘"); // 隐藏软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow 可以结合 InputMethodManager 来在按下回车键后关闭软键盘,改善用户体验。这些方法在实现上可以灵活组合,具体使用哪种方式取决于你需要实现的功能类型。
= null) { InputMethodManager imm = InputMethodManager.getInstance();//2 if (imm 注释2处得到InputMethodManager实例,如果InputMethodManager实例不为null则在注释3处调用InputMethodManager的windowDismissed方法来结束