android强制隐藏软键盘以及取消EditText的焦点
16lz
2021-01-24
直接上代码:
/** * 切换软键盘的状态 * 如当前为收起变为弹出,若当前为弹出变为收起 */private void toggleInput(Context context){ InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }/** * 强制隐藏输入法键盘 */private void hideInput(Context context,View view){ InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0); }/** * root为需要点击取消edittext焦点的layout */ root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { root.setFocusable(true); root.setFocusableInTouchMode(true); root.requestFocus(); return true; }/** * 如果界面较为简单 */InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_DOWN){ if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){ manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } return super.onTouchEvent(event); }
更多相关文章
- Android(安卓)AlertDialog 无法弹出输入法
- 通过设置android:imeOptions来改变软键盘Enter键图标
- Missing styles. Is the correct theme chosen for this layout?
- 在线音乐播放器
- android 摄像头(我想在类里面直接编辑UI界面,但是老是提示空指针,麻
- 界面有Edittext时有些手机进入界面会自动弹出键盘,消除自动弹出键
- 【Android(安卓)Socket专题】:UDP通信客户端app的demo的实现
- Android(安卓)Studio SDK 各版本下载方法
- android学习——处理 EditText 右下角为Done,回车 的情况