一、EditText的焦点获取,需要在EditText的父级空间加上以下属性:
android:focusable="true"
android:focusableInTouchMode="true"
二、点击屏幕非?EditText 处,是软键盘自动关闭:(直接将以下方法复制到需要用到的Activity)
1.判定是否需要关闭软键盘
/**
* 判定是否要关闭软键盘
*/
private boolean isHideSoftKeyboard(View view, MotionEvent event) {
if (view instanceof EditText) {
int[] l = {0, 0};
view.getLocationInWindow(l);
int left = l[0], top = l[1], bottom = top + view.getHeight(), right = left + view.getWidth();
return !(event.getX() > left) || !(event.getX() < right) || !(event.getY() > top) || !(event.getY() < bottom);
}
return false;
}
2.关闭软键盘方法
/**
* 关闭软键盘
*/
private void HideSoftKeyboard(IBinder binder) {
if (binder!= null) {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
?3.重写Activity的 dispatchTouchEvent (触摸事件分发方法)
/**
* 触摸事件分发方法
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View view = getCurrentFocus();
if (isHideSoftKeyboard(view, event)) {
HideSoftKeyboard(view.getWindowToken());
view.clearFocus();
}
}
return super.dispatchTouchEvent(event);
}
|