点击屏幕其他地方让软键盘消失

/*

在获取不到焦点的时候隐藏软键盘

*/

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getCurrentFocus() != null
&& getCurrentFocus().getWindowToken() != null) {
SoftInputMethodManager.hide(this);
}
}
return super.onTouchEvent(event);
}

 

public class SoftInputMethodManager {
/**
* 隐藏软键盘
*
* @param context
*/
public static void hide(Activity context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(context.getWindow().getDecorView()
.getWindowToken(), 0);
}
}

}

posted @ 2015-02-27 15:06  Wang_Daxiong  阅读(515)  评论(0编辑  收藏  举报