1
I would like my application to force the closure of the mobile keyboard whenever the user presses a button (which is above and next to Edit).
1
I would like my application to force the closure of the mobile keyboard whenever the user presses a button (which is above and next to Edit).
2
Inside the button click, in onClickListener, put this code:
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
if(imm.isActive())
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
This will cause the device’s virtual keyboard to be closed.
0
Call this method:
/**
* Esconda o teclado
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Mostre o teclado
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
On the basis of Soen’s excellent reply: https://stackoverflow.com/a/18977227/6736591
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
It worked, but if you click the button the keyboard is closed, the keyboard is opening. How can I create an if to check this case? For example only run this command if the keyboard is open.
– Lucas Fernando
@Lucasfernando, I changed the example to be able to put the conditional.
– Grupo CDS Informática
I implemented the conditional and still the same problem occurs
– Lucas Fernando
Após algumas pesquisas sobre este InputMethodManager consegui solucionar o problema alterando a linha "imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);" para "imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS)" I honestly don’t know the difference, but it solved my problem. If anyone can explain to me why this way works, or if this is really the right way to use this code...
– Lucas Fernando