How to hide the soft keyboard when leaving the edit field?

After the user enters the input into the keyboard input unit, I want the keyboard to hide, so I see the results at the bottom of the screen. I tried this in my onCreate method, but it does not work. What am I missing?

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditTextbox.getWindowToken(), 0); 
+4
source share
5 answers

It depends on what the focus currently has ... if its another editText that focuses, then that might trigger the keyboard ... try explicitly pointing the focus to another element ...

+1
source

You can make the keyboard hide like this:

 Window window = getWindow(); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

I would get a window in the onCreate method and in the onFocusChanged () or OnKeyListener () method to hide the keyboard.

+3
source

Have you installed a key listener?

You really don’t indicate how you know that the user has entered the text, so I assume that they press the enter button on the soft keyboard. This is how I handle this type of script. I use this both in dialogue and in action with success. Hope this helps.

 this.setOnKeyListener(new OnKeyListener() { /** * This listens for the user to press the enter button on * the keyboard and then hides the virtual keyboard */ @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ( (event.getAction() == KeyEvent.ACTION_DOWN ) && (keyCode == KeyEvent.KEYCODE_ENTER) ) { // hide virtual keyboard InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(sessionTag.getWindowToken(), 0); return true; } return false; } }); 
+2
source

You can extend the EditText class and use your code in the onFocusChanged() method if the focused argument is false .

+1
source

Follow this answer. call the method after you set theContentView to your activity.

fooobar.com/questions/27514 / ...

0
source

Source: https://habr.com/ru/post/1341215/


All Articles