How disable softkeyboard in WebView

后端 未结 6 669
被撕碎了的回忆
被撕碎了的回忆 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:44

    A kind of hack solution which came to my mind but still does what is required - hides the keyboard so user will not see it:

    public class WebViewEx extends WebView {
    
        private Handler mHandler;
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            ensureKeyboard();
            return super.onTouchEvent(event);
        }
    
        private void ensureKeyboard() {
            if(mHandler == null){
                mHandler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        closeKeyboard();
                        sendEmptyMessageDelayed(0, 10);
                    }
                };
            }
            mHandler.removeCallbacksAndMessages(null);
            mHandler.sendEmptyMessage(0);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mHandler.removeCallbacksAndMessages(null);
                }
            }, 300);
        }
    
        private void closeKeyboard() {
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
        }
    
    }
    

    Feel free to play with the delays to minimize overhead because of rapid call of a function during the predefined period. I believe optimal values may vary depending on device.

提交回复
热议问题