Webview有焦点,但虚拟键盘没有显示

时间:2017-05-20 17:22:11

标签: android webview

我试过这个:Tapping form field in WebView does not show soft keyboard

我在create上调用的设置函数是

public void begin() {
    EditText t = (EditText) findViewById(R.id.ccentry);
    t.setSelected(false);
    t.clearFocus();

    final WebView webview = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

    View.OnTouchListener l =  new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    Log.v("biscuit-focus", "focus");
                    if (!v.hasFocus()) {
                        Log.v("biscuit-focus-down", "focusDOWN");
                        v.requestFocus(View.FOCUS_DOWN);
                    }
                    break;
            }
            return false;
        }
    };

    webview.setOnTouchListener(l);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            printDebug();
            webview.requestFocus(View.FOCUS_DOWN);
            if (!madeTimer) {
                madeTimer = true;
                timer = new Timer();

                timer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        handler.obtainMessage(1).sendToTarget();
                    }
                }, 5000, 5000);


            }
        }
    });

    webview.loadUrl("myapp.com");
}

当我向下滚动并点击文本字段时,我可以输入USB键盘。 biscuit-focus日志显示但不显示bicuit-focus-down

这是在运行Lollipop的Pine64上。

2 个答案:

答案 0 :(得分:0)

您好尝试在清单中更改此内容

<activity
            android:name=".ActivityName"
            android:windowSoftInputMode="adjustResize"
            android:screenOrientation="portrait"></activity>

答案 1 :(得分:0)

您可以添加以下代码:

它将100%有效。

 webview.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            view.removeOnPreDrawListener(this);
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

            // !Pay attention to return `true`
            // Chet Haase told to 
            return true;
        }
    });

webview.requestFocus(View.FOCUS_DOWN);
祝你好运!!!