有时候会遇到一个需求,点击屏幕其他位置,把已经打开的键盘隐藏掉。比如登录, 注册之类的常见页面.
在onclick 中隐藏是可以实现的,但是在页面非常的复杂 onclick 方法很多时,就会有逻辑处理冲突的情况。
下面直接上代码:
//打开或关闭软键盘
public classKeyBoardUtils {
/**
* 打开软键盘
*
*@parammEditText
*@parammContext
*/
public static voidopenKeybord(EditText mEditText, Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}
/**
* 关闭软键盘
*
*@parammEditText
*@parammContext
*/
public static voidcloseKeybord(EditText mEditText, Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(),0);
}
/**
* des:隐藏软键盘,这种方式参数为activity
*
*@paramactivity
*/
public static voidhideInputForce(Activity activity) {
if(activity ==null|| activity.getCurrentFocus() ==null)
return;
((InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 打开键盘
**/
public static voidshowInput(Context context, View view) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm !=null) {
view.requestFocus();
imm.showSoftInput(view,0);
}
}
}
BaseActivity
public class BaseActivity extends AppCompatActivity {
//region软键盘的处理
/**
* 清除editText的焦点
*
*@paramv焦点所在View
*@paramids输入框
*/
public void clearViewFocus(View v,int... ids) {
if(null!= v &&null!= ids && ids.length>0) {
for(intid : ids) {
if(v.getId() == id) {
v.clearFocus();
break;
}
}
}
}
/**
* 隐藏键盘
*
*@paramv焦点所在View
*@paramids输入框
*@returntrue代表焦点在edit上
*/
public boolean isFocusEditText(View v,int... ids) {
if(vinstanceofEditText) {
EditText tmp_et = (EditText) v;
for(intid : ids) {
if(tmp_et.getId() == id) {
return true;
}
}
}
return false;
}
//是否触摸在指定view上面,对某个控件过滤
public boolean isTouchView(View[] views, MotionEvent ev) {
if(views ==null|| views.length==0)return false;
int[] location =new int[2];
for(View view : views) {
view.getLocationOnScreen(location);
intx = location[0];
inty = location[1];
if(ev.getX() > x && ev.getX() < (x + view.getWidth())
&& ev.getY() > y && ev.getY() < (y + view.getHeight())) {
return true;
}
}
return false;
}
//endregion
//region 右滑返回上级
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_DOWN) {
if(isTouchView(filterViewByIds(), ev))return super.dispatchTouchEvent(ev);
if(hideSoftByEditViewIds() ==null|| hideSoftByEditViewIds().length==0)
return super.dispatchTouchEvent(ev);
View v = getCurrentFocus();
if(isFocusEditText(v, hideSoftByEditViewIds())) {
//隐藏键盘
KeyBoardUtils.hideInputForce(this);
clearViewFocus(v, hideSoftByEditViewIds());
}
}
return super.dispatchTouchEvent(ev);
}
/**
* 传入EditText的Id
* 没有传入的EditText不做处理
*
*@returnid 数组
*/
public int[] hideSoftByEditViewIds() {
return null;
}
/**
* 传入要过滤的View
* 过滤之后点击将不会有隐藏软键盘的操作
*
*@returnid 数组
*/
public View[] filterViewByIds() {
return null;
}
}
我们只要在hideSoftByEditViewIds()方法中直接return int数组即可,int值为editText的id,然后就完成了.
@Override
public int[] hideSoftByEditViewIds() {
int[] ids = {R.id.et_phone, R.id.et_check_code, R.id.et_city_code};
return ids;
}
设置点击不需要隐藏的View
@Override
public View[] filterViewByIds() {
View[] views = {xxxx,xxxxx};
return views;
}