DevilUtil.java 1.73 KB
package com.cnlive.gundam.user.util;

import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by luxiaoman on 2017/2/16.
 */

public class DevilUtil {
    /**
          * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时没必要隐藏
          * 
          * @param v
          * @param event
          * @return
          */
    public static boolean isShouldHideInput(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{
                // hideSoftInput(v.getWindowToken());
                return true;
                }
            }
        // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点
        return false;
        }

            /**
          * 多种隐藏软件盘方法的其中一种
          * 
          * @param token
          */
            public static void hideSoftInput(IBinder token, InputMethodManager im){
        if(token!=null){
            im.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
}