android打开和隐藏软键盘

来源:互联网 发布:3d展示软件 编辑:程序博客网 时间:2024/05/17 02:34

前言:这篇博客主要写一下如何在edittext获取焦点的时候弹出软键盘,以及如何强制关闭软键盘。
1.

//让编辑框获取到焦点        EditText.setFocusable(true);        EditText.setFocusableInTouchMode(true);        EditText.requestFocus();//打开软键盘        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

2.强制关闭软键盘

edittext = findViewById(R.id.edittext);InputMethodManager inputMethodManager =                                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                        inputMethodManager.hideSoftInputFromWindow(edittext.getWindowToken(),0);