Android弹出软键盘和关闭软键盘

998 阅读1分钟

前言:没有什么比测试给你提bug进步更快了

在一些有输入框的界面中,我们需要自动弹出软键盘。比如搜索界面,输入框需要自动获取焦点并弹出软键盘。

弹出软键盘代码:


   mEditText.setFocusable(true);
   mEditText.setFocusableInTouchMode(true);
   mEditText.requestFocus();
   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);


关闭软键盘代码


  InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);



参考:blog.csdn.net/devnn/artic…