【转】Android点击空白区域,隐藏输入法软键盘
原文网址:http://www.2cto.com/kf/201505/401382.html
很多时候,我们在使用应用时,会出现输入法软键盘弹出的问题,通常情况下,我们默认会使用户点击返回键或者下一步对软键盘进行隐藏。为了更好的体验,我们可以实现当用户使用完毕软键盘时。点击空白区域即可实现隐藏的功能。效果如图所示:
代码实现
代码块语法遵循标准markdown代码,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<code class = "language-java" hljs= "" > package example.com.jinlin.myapplication; import android.content.Context; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * Created by J!nl!n on 15/5/21. */ public abstract class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); iniView(); } public abstract void iniView(); @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { View v = getCurrentFocus(); if (isShouldHideKeyboard(v, ev)) { hideKeyboard(v.getWindowToken()); } } return super .dispatchTouchEvent(ev); } /** * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏 * * @param v * @param event * @return */ private boolean isShouldHideKeyboard(View v, MotionEvent event) { 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 (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // 点击EditText的事件,忽略它。 return false ; } else { return true ; } } // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点 return false ; } /** * 获取InputMethodManager,隐藏软键盘 * @param token */ private void hideKeyboard(IBinder token) { if (token != null ) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } } } </code> |
当然我们还有更加简单的方法来实现该功能,只需要重写onTouchEvent方法即可。代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
// 点击空白区域 自动隐藏软键盘 public boolean onTouchEvent(MotionEvent event) { if ( null != this .getCurrentFocus()){ /** * 点击空白位置 隐藏软键盘 */ InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); return mInputMethodManager.hideSoftInputFromWindow( this .getCurrentFocus().getWindowToken(), 0 ); } return super .onTouchEvent(event); } |
使用一个BaseActivity进行一些处理公共操作,其他Activity均继承自该基类Activity即可,则所有界面均可实现点击空白区域,隐藏软键盘。
android 点击关闭软键盘
原文网址:http://www.2cto.com/kf/201412/360428.html
在项目中,editText获取焦点后,会自动弹出软键盘,关闭的时候一般需要按返回键或者点击软键盘上的按钮,
即使当前activity已经finish掉,软键盘依然存在,会影响用户的体验。
网上目前有很多很详细的办法,比如点击其他空白区域,软键盘就会消失之类的方法,我们项目中没有要求这个,要求的是只要
不遮挡其他操作,还有当前Activity关闭掉后软键盘消失就行,
今天给大家分享两个办法:
1
2
3
4
5
6
7
8
9
10
11
|
//此方法,如果显示则隐藏,如果隐藏则显示 private void hintKbOne() { InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); // 得到InputMethodManager的实例 if (imm.isActive()) { // 如果开启 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); } } |
1
2
3
4
5
6
7
8
9
|
//此方法只是关闭软键盘 private void hintKbTwo() { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive()&&getCurrentFocus()!= null ){ if (getCurrentFocus().getWindowToken()!= null ) { imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } |
当需要点击事件关闭软键盘的时候只需要调用方法就好。
【转】Android点击空白区域,隐藏输入法软键盘的更多相关文章
- 【移动开发】 Android隐藏输入法软键盘的一些说明
刚刚在写一个仿微信的Android聊天软件,在编写的过程中,发现一个严重的BUG---当用户点击输入框用软键盘输入文本的时候点击了"返回好友列表"的按钮,返回到好友列表时软键盘无法 ...
- Android点击其他任意位置收起软键盘
在Android应用开发中,经常出现这样的需求,用户在输入文字的过程中,可能不想继续输入了,通过滑动或者点击其他位置(除软键盘和EditText以外的任何位置),希望能够自动收回键盘,这个功能可能有些 ...
- 实例:vue中点击空白区域关闭某个div图层
<template> <div class="search" ref="searchMain"> <el-input v-mode ...
- JQ 点击指定文本框显示div。点击其他区域隐藏DIV
<input id="username" type="text" style="width:90%;margin-top: 40px;" ...
- js的事件冒泡和点击其他区域隐藏弹出层
一.前言 在编写页面的时候,我们经常使用到弹出层.对于弹出层,原本的意义就是增加与用户的交互,提升用户的好感度.如果弹出层都没有较好的体验,那何谈通过交互来提升好感... 首先提出几个弹出层的注意点: ...
- Android之弹出/隐藏系统软键盘
Android弹出/隐藏系统软键盘的代码如下: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT ...
- android:windowSoftInputMode属性;界面关闭后软键盘不隐藏的解决方法;
stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activit ...
- ios键盘弹起 body的高度拉长,页面底部空白问题。ios软键盘将页面抵到上面后,关闭软键盘页面不回弹的问题。
js 监听ios手机键盘弹起和收起的事件 /* js 监听ios手机键盘弹起和收起的事件 */ document.body.addEventListener('focusin', () => { ...
- Android popupwindow 失去焦点或者点击空白区域时消失的解决方法
先来看下Android API 的这个Methods: public void setOutsideTouchable (boolean touchable) Controls whether the ...
随机推荐
- [Angular 2] Exposing component properties to the template
Showing you how you can expose properties on your Controller to access them using #refs inside of yo ...
- (转)Css样式兼容IE6,IE7,FIREFOX的写法
根据FF和IE对一些符号识别的差异,我们可以单独对FF以及IE定义样式,例子: 区别IE6与FF: background:orange;*background:blue; 区别I ...
- 经验分享:CSS浮动(float,clear)通俗讲解(转载)
很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不及待的分享给大家. 写在前面的话: 由于CSS内容比较多 ...
- Google2016开发者大会
Android主讲: 一.吴晶:android笔记博主(博客:http://www.race604.com/) 主题:Android低功耗蓝牙(BLE)实践 低功耗蓝牙在可穿戴和智能家居里边用的比较多 ...
- XML样本(格式没区别,但是一个有结果 一个没结果)
xml样本01<orderlist> <order> <orderid>1</orderid> <ord ...
- AFN发送请求失败
发送请求出现这个错误 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Coc ...
- C# == 和equals()区别
如以下代码: ? 1 2 3 4 5 int age = 25; short newAge = 25; Console.WriteLine(age == newAge); //true Consol ...
- Objective-C中的协议(Protocol)和类别(Category)
1.什么是协议? 2.协议与类别的声明和使用 1.什么是协议? 在Objective-C中,不支持多继承,即不允许一个类有多个父类,但是OC提供了类似的实现方法,也就是协议.协议有点类似于Java里的 ...
- location对象位置操作,进行跳转
location位置操作,进行跳转 location.assign("http://www.baidu.com") 跳转,打开新的url 等价于,将location.href或wi ...
- Windows下Apache部署Django过程记录
Win7/Apache/Python2.7/Django1.9部署Web 环境: Windows7 Apache httpd-2.4.16-win64-VC14 Python2.7.11 Djan ...