简体   繁体   中英

Still visible soft keyboard

I have curious case of still visible soft-keyboard. Simple flow. Activity without any edittexts, where soft keyboard is hidden. From manifest:

<activity
        android:name=".activities.MainActivity"
        android:theme="@style/MainActivity"
        android:windowSoftInputMode="stateAlwaysHidden" />

...and also checked from code

        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

In this activity I have a fragment which contains webview. I have added touchlistener to this webview which send 'space' key to it.

protected View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Instrumentation inst = new Instrumentation();
                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_SPACE);
        }
        return false;
    }
};

It handles start/pause effect, where I have vimeo video-content of that webview. Previously I tried to communicate with vimeo video player by javascript handler, but it wasn't work. Sending key event is really simple solution and works great on all devices except one. On dell t01c when touchlistener is invoked, software keyboard appears. I can hide it with simple

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);

but it ruins immersive mode of activity - navigation bar shows and disappears. How to block keyboard permanently? Has anyone had similar problem?

Try this

put this static method in your Utils class

 public static void hideKeyboard(Activity activity) {
            try {
                InputMethodManager input = (InputMethodManager) activity
                        .getSystemService(Activity.INPUT_METHOD_SERVICE);
                input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Call this method whereever you want to hide the keyboard by Utils.hideKeyboard(YOURACTIVITY.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM