当单击按钮并且输入被聚焦时,如何在连接了USB HID设备的webview中显示软键盘?

Ben*_*une 26 android

我有一个USB条码扫描器,它通过USB OTG插入设备.因为它被选为键盘HID设备,所以当输入被聚焦时,软键盘被禁用.

我目前有一个从JavaScript触发键盘的方法.

class JsObject {
    @JavascriptInterface
    public void InvokeKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(findViewById(R.id.webView), InputMethodManager.SHOW_FORCED);
    }
}

webview.addJavascriptInterface(new JsObject(), "Android");
Run Code Online (Sandbox Code Playgroud)

我基本上想要它在webview中的以下动作上打开键盘.

const button = document.querySelector('.fire-keyboard');
button.addEventListener('click', () => Android.InvokeKeyboard());
Run Code Online (Sandbox Code Playgroud)

它可以在从webview手动调用时工作,但我希望能够单击触发它的按钮,然后单击按钮会丢失输入框焦点.

只是添加,我不希望键盘在聚焦输入框时显示,只显示单击按钮时触发它.默认情况下,它显示焦点.

Ben*_*une 7

结果我只需要打开这个硬件.

在此输入图像描述


Zub*_*med 6

可能是因为您使用的设备.我根据你的场景制作了一个样本,并在Nexus5我正在使用的设备上成功运行它.

只需document.getElementById("mytextfield").focus();在JS代码中使用即可.

示例HTML代码

<!DOCTYPE html>
<html>
<head>
</head>

<body>

<form name="myform">
<input type="text" id="mytextfield">
</form>

<input type="button" value="Click Me!" onClick="executeAndroidCode()" />

<script type="text/javascript">
    function executeAndroidCode() {
        Android.execAndroid();
        document.getElementById("mytextfield").focus(); //this line did the trick after click on button
    }
</script>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

将此文件保存在名为的资产中 index.html

JavaScriptInterface

public class WebAppInterface 
    {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void execAndroid() {
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(findViewById(R.id.webView1), 
                    InputMethodManager.SHOW_FORCED);
        }
    }
Run Code Online (Sandbox Code Playgroud)

onCreate Code

WebView wv = (WebView) findViewById(R.id.webView1);
        wv.addJavascriptInterface(new WebAppInterface(this), "Android");

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

        wv.loadUrl("file:///android_asset/index.html");
Run Code Online (Sandbox Code Playgroud)

它在我身边工作正常我正在使用Nexus5.虽然光标不会显示在屏幕截图中,但是当我运行应用程序时它会闪烁.

在此输入图像描述


Axe*_*elH 2

我现在无法测试它,但你可以简单地使用 javascriptinterface 来调用它:

InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();
Run Code Online (Sandbox Code Playgroud)

这应该使用当前键盘(输入设备)打开对话框。由于蓝牙扫描仪取代了当前的键盘,我不确定您是否可以显示软键盘。

使用相同的方法,如果需要,用户将切换回扫描仪。

当我回到家时,我会尝试一下,即从现在开始的 6 或 7 小时后,但我可以自由地尝试自己;)