0

I'd like to hide the Android native keyboard when I do click over an <input type=text> without plugins and without lost the focus of the input.

With JavaScript I can detect when the keyboard show or hide, but I'd like to hide when I want it.

The backbutton hides the keyboard when it is showing, so, I think that I could trigger the backbutton click event when I detect a showkeyboard event, but it doesn't work.

document.addEventListener("showkeyboard", 
    function(){ 
        alert("Keyboard is ON"); 
        $("backbutton").trigger('click'); // Doesn't work effect
    }, false);

document.addEventListener("hidekeyboard", 
    function(){ 
        alert("Keyboard is OFF");

    }, false);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vadillo15
  • 5
  • 3
  • readonly-attribute works and it should still be able to receive focus as it states here: http://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-ht – Blauharley Mar 13 '15 at 22:17

2 Answers2

0

first in your mainactivity import :

    import android.view.inputmethod.InputMethodManager;
    import android.webkit.JavascriptInterface;

Then add a javascriptinterface in onCreate Method :

   super.appView.addJavascriptInterface(new JSInterface(), "testInterface");

After that add the follwing code in Mainactivity class :

    @JavascriptInterface
    public void hideKeyboard() {
        runOnUiThread(new Runnable() {

            public void run() {
                try {
                    InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Service.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(<MainactivityClass>.this.getCurrentFocus().getWindowToken(), 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Now from HTML, call the native hideKeyboard() method in the input box like this way :

    <input type="text" onfocus="window.testInterface.hideKeyboard()" />
Banik
  • 911
  • 6
  • 10
0

Try using directly

document.getElementById('input_field_to_focus').focus();

This adds focus to the input field but does not invoke the soft keyboard. The keyboard open event is not triggered on Android unless it is from a click event.

Refer the link:http://code.google.com/p/android/issues/detail?id=27438

Shruti
  • 43
  • 8