Android點選EditText之外的地方隱藏軟鍵盤同時使EditText失去焦點
阿新 • • 發佈:2019-01-24
1.只是隱藏軟鍵盤:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
assert v != null;
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否則所有的元件都不會有TouchEvent了
return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
//獲取輸入框當前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
return !(event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom);
}
return false;
}
使用上面連結中的方法,只能隱藏鍵盤的使不能使EditText失去焦點,所以做出修改,使用clearFocus()清空EditText的焦點。
2.隱藏軟鍵盤同時使EditText失去焦點(clearFocus())
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
assert v != null;
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
/** here */
v.clearFocus();
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否則所有的元件都不會有TouchEvent了
return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
//獲取輸入框當前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
return !(event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom);
}
return false;
}
使用此方法之後當我點選狀態列的時候,可以實現上面的需求, 但是 會有別的EditText獲取到焦點,具體原因沒有找到。最後通過讓根佈局獲取焦點的方法解決。
3.隱藏軟鍵盤同時使EditText失去焦點(requestFocusFromTouch())
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
assert v != null;
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
/** here */
mRootView.setClickable(true);
mRootView.setFocusable(true);
mRootView.setFocusableInTouchMode(true);
mRootView.requestFocusFromTouch();
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否則所有的元件都不會有TouchEvent了
return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
//獲取輸入框當前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
return !(event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom);
}
return false;
}
然後我把此方法寫在EditBaseActivity中,EditBaseActivity 繼承自 BaseActivity。所有有EditText的Activity都可以直接繼承此類。
public class EditBaseActivity extends BaseActivity {
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
assert v != null;
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
mRootView.setClickable(true);
mRootView.setFocusable(true);
mRootView.setFocusableInTouchMode(true);
mRootView.requestFocusFromTouch();
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否則所有的元件都不會有TouchEvent了
return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = {0, 0};
//獲取輸入框當前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
return !(event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom);
}
return false;
}
}