需求:
使用代码实现Android的输入框EditText对键盘的关闭弹出的实现。
代码:
/** * 打开键盘 * * @param editText 操作的输入框 */ public static void openKeyboard(final EditText editText) { //设置可获得焦点 editText.setFocusable(true); editText.setFocusableInTouchMode(true); //请求获得焦点 editText.requestFocus(); //调用系统输入法 Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputManager = (InputMethodManager) editText .getContext().getSystemService( Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(editText, 0); } }, 100); } /** * 关闭键盘 * * @param editText 操作的输入框 */ public static void closeKeyboard(EditText editText) { //关闭键盘 InputMethodManager imm = (InputMethodManager) editText .getContext().getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); }