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

来源:互联网 发布:美国直播软件 编辑:程序博客网 时间:2024/05/01 20:51

写在公共继承类里面,方便操作。

这个公共继承类原本还有一个显示加载功能,让我给删除掉了,目前也就一个点击空白区域,隐藏输入法软键盘功能而已。

import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.IBinder;import android.support.v4.app.FragmentActivity;import android.view.MotionEvent;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public abstract class BaseActivity extends FragmentActivity {//获取到前台的Activityprivate static Activity mForegroundActivity = null;public boolean isOnline;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initView();init();}/** * 初始化界面 */protected abstract void initView();/** * 初始化数据 */protected abstract void init();@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();this.mForegroundActivity = this;}@Overrideprotected void onPause() {super.onPause();this.mForegroundActivity = null;}public static Activity getForegroundActivity() {return mForegroundActivity;}/** * 点击空白处关闭软键盘逻辑 * */@Overridepublic 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);}}}


0 0
原创粉丝点击