关于Android键盘隐藏及Edittext光标隐藏
首先拿到当前焦点控件,判断是否隐藏键盘,键盘都隐藏了,光标必然是要隐藏的,所以直接让当前焦点控件直接隐藏就好了
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(ev.getAction()==MotionEvent.ACTION_DOWN)//触摸屏幕
{
//这个是重要的,获取当前焦点控件
View view = getCurrentFocus();
boolean tmp = true;//隐藏 true : false,隐藏 :显示
//判断点击区域是否在控件内部,不在就隐藏,在就显示
if(view!=null&&view instanceof EditText)//汗,万一view是空的咋办,先判断有没有焦点吧
{
int [] outLocation = {0,0};//左上角坐标,控件起始位置
view.getLocationInWindow(outLocation);//@Size(2) int[] outLocation //参考View.getLocationInWindow()和 View.getLocationOnScreen()在window占据全部screen时,两值相同 否则outlocation的值是inWindow<OnScreen,看着用吧
int i = outLocation[0] + view.getHeight();//右下角,控件结束位置
int j = outLocation[1] + view.getWidth();//
//触摸点坐标
float touchX = ev.getX();
float touchY = ev.getY();
//判断是否在区间范围内
if(touchX>outLocation[0]&&touchX<j&&touchY>outLocation[1]&&touchY<i)
tmp = false;
else
tmp = true;
}
else
tmp = true;
//拿到结果了判断下是否隐藏键盘和焦点就行了
if(tmp)
{
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(INPUT_METHOD_SERVICE);
if(inputMethodManager !=null)
{
inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(),0);
}
view.clearFocus();//隐藏光标
}
}
return super.dispatchTouchEvent(ev);
}

浙公网安备 33010602011771号