Button to display and/or hide keyboard

Asked

Viewed 532 times

1

A EditText numeric will receive input by external keyboard, so display the default keyboard, should be optional.

How do I make to:

By clicking on EditText to position the cursor, the keyboard does not open ?

Open/close the keyboard with a separate button ?

2 answers

2

etNumerico.setInputType(InputType.TYPE_NULL);

Try the above to define that it will not call the virtual keyboard. And to show or hide the keyboard by a button like this:

//Mostrar e esconder
 private void funcaoTeclado() {
    View v = getActivity().getCurrentFocus();

    if (v != null) {

        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.toggleSoftInputFromWindow(v.getWindowToken(), InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
}

I’m using getActivity because I’m using it in a fragment, so that part of the code can change according to the context of your app

  • Didn’t work Anderson. But I got it here already. Thanks !

  • Anderson, tested again. Do not open the keyboard is working perfectly. What does not work is show the keyboard. If you can check, thank you.

  • I will edit the answer, I just tested android 8.1.0 and took. Take the test the new way and tell me

  • Now you changed to method "toggle"... was practically the same as mine ! rs Actually I tested yours, it worked, but gave a problem because the etNumerico.setInputType(InputType.TYPE_NULL); he broke my field parameter to be "number". I will post in my detailed reply as it was

  • 1

    Yeah, the only difference is it’s past the show and hide flags. The method I passed before, I use an application for an android tablet 2.3, so it may be the reason not to pick the newest android. I believe the toggle is better.

  • In my case yes, I need the toggle because I’m using a floating button. But thanks for the hand ! ;]

Show 1 more comment

2


I used it as follows:

Show/Hide in the same function (toggleSoftInput):

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

Only hide (hideSoftInputFromWindow):

InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Only display (showSoftInput):

InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
        imm.showSoftInput(this.cod, InputMethodManager.SHOW_IMPLICIT);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.