简体   繁体   English

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

[英]How can I show the soft keyboard in a webview with a USB HID device attached when a button is clicked and an input is focused?

I have a USB barcode scanner which is plugged into the device with USB OTG. 我有一个USB条码扫描器,它通过USB OTG插入设备。 Because this is picked up as a keyboard HID device, the soft keyboard is disabled when inputs are focused. 因为它被选为键盘HID设备,所以当输入被聚焦时,软键盘被禁用。

I currently have a method which triggers the keyboard from JavaScript. 我目前有一个从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");

I'm basically wanting it to open the keyboard on the following action inside the webview. 我基本上想要它在webview中的以下动作上打开键盘。

const button = document.querySelector('.fire-keyboard');
button.addEventListener('click', () => Android.InvokeKeyboard());

It works when called from the webview manually but I want to be able to click a button which triggers it, and clicking the button loses the input box focus. 它可以在从webview手动调用时工作,但我希望能够单击触发它的按钮,然后单击按钮会丢失输入框焦点。

Just to add, I don't want the keyboard to show when focusing an input box, only to show when a button is clicked to trigger it. 只是添加,我不希望键盘在聚焦输入框时显示,只显示单击按钮时触发它。 By default it shows on focus. 默认情况下,它显示焦点。

Turns out I just needed to have this hardware toggle on. 结果我只需要打开这个硬件。

在此输入图像描述

May be it is due to device you are using. 可能是因为您使用的设备。 I made a sample according to your scenario and successfully run it on Nexus5 device which I am using. 我根据你的场景制作了一个样本,并在我正在使用的Nexus5设备上成功运行它。

Just use document.getElementById("mytextfield").focus(); 只需使用document.getElementById("mytextfield").focus(); in JS code which did the trick. 在JS代码中完成了这个伎俩。

Sample html code 示例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>

Save this file in asset named index.html 将此文件保存在名为index.html资产中

JavaScriptInterface 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);
        }
    }

onCreate Code 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");

Its working fine on my side I am using Nexus5 . 它的工作正常我在使用Nexus5 Although cursor won't showed up in screenshot but it is blinking when I run the app. 虽然光标不会显示在屏幕截图中,但是当我运行应用程序时它会闪烁。

在此输入图像描述

I can't test it right now but you could simply use the javascriptinterface to call this : 我现在无法测试它,但你可以简单地使用javascript接口来调用它:

InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();

This SHOULD open the dialog with the current keyboards (input device). 这应该打开当前键盘(输入设备)的对话框。 Since the bluetooth scanner replace the current keyboard, I am not sure you can display the softkeyboard. 由于蓝牙扫描仪取代了当前的键盘,我不确定您是否可以显示软键盘。

With the same method, the user will switch back to the scanner if needed. 使用相同的方法,用户将根据需要切换回扫描仪。

I will try it when I 'll be back home, in 6 or 7hours from now but fell free to try yourself ;) 我会在我回家的时候尝试它,从现在开始的6或7小时但是可以自由尝试自己;)

Try this 

inputBox.setOnFocusChangeListener(new OnFocusChangeListener() {
   @Override
   public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus) {
         getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
      }
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 强制软键盘在HTML的焦点输入上显示 - Force soft keyboard to show on focused input in HTML 聚焦edittext时如何显示软键盘 - How to show soft-keyboard when edittext is focused 聚焦MultiAutoCompleteTextView时如何显示软键盘 - How to show soft-keyboard when MultiAutoCompleteTextView is focused 聚焦edittext时如何显示软键盘 - How to show soft-keyboard when edittext is focused 单击文本框时,物理设备上的软键盘不显示 - Soft keyboard on physical device doesn't show when a textbox is clicked 如何在WebView中隐藏软键盘? - How can I hide the soft keyboard in WebView? 如何在焦点 EditText 之外单击时隐藏软键盘,但如果单击另一个 EditText 则不隐藏? - How to make the soft keyboard hide when clicked outside of the focused EditText BUT not if another EditText is clicked? 即使连接了输入设备,也显示用于HTML字段的Android软键盘 - Showing Android soft keyboard for an HTML field even when an input device is attached 当设备处于横向模式时显示软键盘 - Show soft keyboard when the device is landscape mode 当软键盘显示并通过Adjust Pan隐藏在Phonegap / Android中时,如何获取键盘事件 - How can I get a keyboard event when soft keyboard show's & hide in Phonegap/Android with Adjust Pan
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM