How disable softkeyboard in WebView

后端 未结 6 677
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 12:26

I need disable open softkeyboard in my WebView and in all edittexts in WebView (I do not access to thay because its is in WebView).

I try use \'android:windowSoftInp

6条回答
  •  甜味超标
    2020-12-06 12:49

    This answer is for those who do not want the keyboard to pop up, but still want to let the user interact with the input box. Either by hardware keyboards or touch cursor

    You could take into consideration of an approach from both Javascript and Android combined.

    Case - we have a WebView (In MainActivity) that has input boxes and we don't want Android to pop the soft keyboard after the user click the textbox

    Below is how to achieve hiding: We need to use both Javascript and Java to achieve this on Android:


    First on Javascript side

    We can create a JS file to handle the webpage displayed in the WebView, and apply it to the webview by calling this in the onCreate of the MainActivity i.e:

    ExtendedWebView webView = findViewById(R.id.webview_id);
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new MyJavascript(), "_my_javascript");
    

    We can then have the JS checking each of the input boxes on the webpage displayed. And if the input box is the type we are interested in we can then apply this function to the input_box we are interested in:

    function hide_soft_keyboard(input_box) {
          input_box.addEventListener("click", function() {
                  input_box.blur(); // very important call
                  input_box.focus();// very important call x2
          });
    }
    

    The above code, force OnClick event of the input box to be out of focus first and then focus. And this is the key to hide the keyboard.

    Second on Java side

    Now that we have the JS part taken care of, we need to extend the WebView we are using to override a specific method provided by the WebView class from the framework:

    @Override 
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            hideKeyboard();
            return super.onCreateInputConnection(outAttrs);
    }
    

    What this method does is to create an input connected to the input_box in the HTML webpage displayed in webview, and this method is called the first time you click an input box on the webview. However, this method will only be called once before the input connection is established, which means if you call hideKeyboard() in this method, it will only work the first time user clicks it, but not the second time.

    BUT, together with the JS change we have earlier, hideKeyboard() will work... How?

    Since in the JS file we created earlier we forced the onClick of the input box to trigger a blur() call, which closes the current input connection, and followed by a focus() call to let android focus on to the same input_box again, this will force the onCreateInputConnection() method to be called again, which triggers the hideKeyboard() method, allowing user to interact with the input box without the soft keyboard popping out, which successfully answered this question: Disable SoftKeyboard inside a WebView

    private void hideKeyboard() {
            post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) imm.hideSoftInputFromWindow(getRootView().getWindowToken(), 0);
                }
            });
    }
    

    Source: I worked on a device with hardware num keys, but has no physical qwerty keyboard, and found out this solution when trying to disable soft keyboard popping out only when input box type is limited to numbers.

提交回复
热议问题