Android 点击空白处自动隐藏输入法
2015-09-21 18:03
525 查看
方法1: 简单但有时会无效 InputMethodManager manager= (InputMethodManager) 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); } 方法2: 完美适用activity与fragment // -------------------------------------隐藏输入法----------------------------------------------------- // 获取点击事件 @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub if (ev.getAction() == MotionEvent.ACTION_DOWN) { View view = getCurrentFocus(); if (isHideInput(view, ev)) { HideSoftInput(view.getWindowToken()); } } return super.dispatchTouchEvent(ev); } // 判定是否需要隐藏 private boolean isHideInput(View v, MotionEvent ev) { if (v != null && (v instanceof EditText)) { int[] l = { 0, 0 }; v.getLocationInWindow(l); int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth(); if (ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom) { return false; } else { return true; } } return false; } // 隐藏软键盘 private void HideSoftInput(IBinder token) { if (token != null) { InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } }
亲测,第一种方法,在项目中很少可以有效,大部分情况是无效的,但是选用第二种方法后,全部有效
相关文章推荐
- android studio修改项目名,模块名,包名
- 移动环境下push技术开发(01-androidpn环境的配置)
- Android获取通话记录
- androidpn的学习研究(二)androidpn-server服务端启动过程的理解分析
- android vector
- Android 无缝轮播实现——本地加载图片轮播(一)
- android中跨进程通讯的4种方式
- Android应用集成支付宝接口的简化
- 【Material Design】Material Design 开发利器:Android Design Support Library 介绍
- Android程序中启动另一个activity
- Android中使用Bitmap会出现OutOfMemory的原因(转)
- Ubuntu 12.04(64位)安装Android Studio
- 【Android Studio】制作清晰Gif截图以作为项目的运行效果
- Android——启动过程详解
- Android 自定义TitleBar替换ActionBar的另类方式
- android string.xml中%1$s、%1$d等的用法
- 【内存优化】(MDCC技术大咖秀)Android内存优化之OOM
- Android——4.2.2 源码目录结构分析
- Android应用的耗电量统计
- Android TV ListView通过遥控器上下键,平滑移动item