Android: dynamically close virtual keyboard though code?

How can you dynamically close the virtual keyboard with code? All I want to do is close it when the user clicks the OK button, because it does not close, even if the button now has focus.

+4
source share
3 answers

Hi, I got another solution to hide the keyboard:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 

Here pass HIDE_IMPLICIT_ONLY at showFlag and 0 at hiddenFlag. He will forcefully close the soft keyboard.

+3
source

You can do this with the following code: this will hide your keyboard

  public boolean dispatchTouchEvent(MotionEvent ev) { View view = getCurrentFocus(); if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) { int scrcoords[] = new int[2]; view.getLocationOnScreen(scrcoords); float x = ev.getRawX() + view.getLeft() - scrcoords[0]; float y = ev.getRawY() + view.getTop() - scrcoords[1]; if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom()) ((InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow ((this.getWin dow().getDecorView().getApplicationWindowToken()), 0); } return super.dispatchTouchEvent(ev); } 
0
source

All Articles