现在的位置: 首页 > 综合 > 正文

android 点击关闭软键盘

2018年05月06日 ⁄ 综合 ⁄ 共 896字 ⁄ 字号 评论关闭

在项目中,editText获取焦点后,会自动弹出软键盘,关闭的时候一般需要按返回键或者点击软键盘上的按钮,

即使当前activity已经finish掉,软键盘依然存在,会影响用户的体验。

网上目前有很多很详细的办法,比如点击其他空白区域,软键盘就会消失之类的方法,我们项目中没有要求这个,要求的是只要

不遮挡其他操作,还有当前Activity关闭掉后软键盘消失就行,

今天给大家分享两个办法:

//此方法,如果显示则隐藏,如果隐藏则显示
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);
	    
	}
}

//此方法只是关闭软键盘
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);
	}			  
 }
}

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

转发请标明地址尴尬  http://blog.csdn.net/jing110fei/article/details/41863821

PS:如果有人转发的话大笑

抱歉!评论已关闭.