很多时候,我们在使用应用时,会出现输入法软键盘弹出的问题,通常情况下,我们默认会使用户点击返回键或者下一步对软键盘进行隐藏。为了更好的体验,我们可以实现当用户使用完毕软键盘时。点击空白区域即可实现隐藏的功能。效果如图所示:

【转】Android点击空白区域,隐藏输入法软键盘_bundle



代码实现

代码块语法遵循标准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);​


​}             ​


​}​


​}​



当需要点击事件关闭软键盘的时候只需要调用方法就好。