软键盘弹出
/** * 打开软键盘 */ protected void showSoftInput(EditText view) { InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE); mInputMethodManager .showSoftInput(view, InputMethodManager.SHOW_FORCED); }
隐藏软键盘
/** * 隐藏软键盘 */ protected void hideSoftInput(EditText view) { InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE); mInputMethodManager .hideSoftInputFromWindow(view.getWindowToken(), 0); }
但有遇到过弹出软键盘没反应的情况,经过各种查找终于在https://www.jianshu.com/p/6fd9aca68218这篇文章得到解决。
是因为我的布局需要改变所以无法弹出输入法。需要做延时操作。
mInput.setVisibility(View.VISIBLE); mInput.setFocusable(true); mInput.setFocusableInTouchMode(true); mInput.requestFocus(); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { InputMethodManager imm = (InputMethodManager) mInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mInput, InputMethodManager.SHOW_FORCED); } }, 300);问题解决。