話不多說直接復(fù)制到你的baseAty就可以
/**
* 點(diǎn)擊頁面空白處時(shí)谭胚,讓鍵盤消失
*
* @param event 事件
* @return boolean
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
? ? if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getCurrentFocus() !=null && getCurrentFocus().getWindowToken() !=null) {
if (getCurrentFocus().getWindowToken() !=null) {
mInputMethodManager.hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(),
? ? ? ? ? ? ? ? ? ? ? ? InputMethodManager.HIDE_NOT_ALWAYS);
? ? ? ? ? ? }
}
}
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
? ? ? ? if (isShouldHideKeyboard(v, ev)) {
hideKeyboard(v.getWindowToken());
? ? ? ? }
}
return super.dispatchTouchEvent(ev);
}
/**
* 根據(jù)EditText所在坐標(biāo)和用戶點(diǎn)擊的坐標(biāo)相對(duì)比,來判斷是否隱藏鍵盤翔悠,因?yàn)楫?dāng)用戶點(diǎn)擊EditText時(shí)則不能隱藏
*
* @param v? ? View
* @param event 事件
* @return boolean
*/
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
if (v !=null && (vinstanceof EditText)) {
int[] l = {0, 0};
? ? ? ? v.getLocationInWindow(l);
? ? ? ? int left = l[0],
? ? ? ? ? ? ? ? top = l[1],
? ? ? ? ? ? ? ? bottom = top + v.getHeight(),
? ? ? ? ? ? ? ? right = left + v.getWidth();
? ? ? ? return !(event.getX() > left) || !(event.getX() < right)
|| !(event.getY() > top) || !(event.getY() < bottom);
? ? }
// 如果焦點(diǎn)不是EditText則忽略厂汗,這個(gè)發(fā)生在視圖剛繪制完委粉,第一個(gè)焦點(diǎn)不在EditText上,和用戶用軌跡球選擇其他的焦點(diǎn)
? ? return false;
}
/**
* 獲取InputMethodManager娶桦,隱藏軟鍵盤
*
* @param token1 Ibinder
*/
private void hideKeyboard(IBinder token1) {
if (token1 !=null) {
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
? ? ? ? im.hideSoftInputFromWindow(token1, InputMethodManager.HIDE_NOT_ALWAYS);
? ? }
}