Guest User

Untitled

a guest
Jan 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public boolean dispatchTouchEvent(MotionEvent ev) {
  2. View v = getCurrentFocus();
  3.  
  4. if (v != null &&
  5. (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
  6. v instanceof EditText &&
  7. !v.getClass().getName().startsWith("android.webkit.")) {
  8. int scrcoords[] = new int[2];
  9. v.getLocationOnScreen(scrcoords);
  10. float x = ev.getRawX() + v.getLeft() - scrcoords[0];
  11. float y = ev.getRawY() + v.getTop() - scrcoords[1];
  12.  
  13. if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
  14. hideKeyboard(this);
  15. }
  16. return super.dispatchTouchEvent(ev);
  17. }
  18.  
  19. public static void hideKeyboard(Activity activity) {
  20. if (activity != null && activity.getWindow() != null) {
  21. InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  22. assert imm != null;
  23. imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
  24. }
  25. }
Add Comment
Please, Sign In to add comment