1 public static void hideKeyboard(View view){
2 InputMethodManager imm = (InputMethodManager) view.getContext()
3 .getSystemService(Context.INPUT_METHOD_SERVICE);
4 if (imm != null) {
5 imm.hideSoftInputFromWindow(view.getWindowToken(),0);
6 }
7 }
8
9
10 public static void focusControlAndShowKeyboard(View view) {
11 // 编辑框自动获取焦点并弹出输入法
12 view.requestFocus();
13 Timer timer = new Timer(); //设置定时器
14 timer.schedule(new TimerTask() {
15 @Override
16 public void run() { //弹出软键盘的代码
17 InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
18 imm.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
19 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
20 }
21 }, 300); //设置300毫秒的时长
22 }
在显示软键盘的时候,如果不设置延迟,很多时候都无法弹出软键盘。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/281189.html