简体   繁体   English

Android - 以编程方式显示/隐藏 WebView 中的软键盘

[英]Android - show/hide soft keyboard in WebView programmatically

I need to show/hide the Android soft keyboard programmatically in a WebView.我需要在 WebView 中以编程方式显示/隐藏 Android 软键盘。 The user has a button to show/hide it.用户有一个按钮来显示/隐藏它。 I've read a few posts on how to hide it but none about a toggle feature, so that the user can hide or show it on demand and use only the hardware keyboard where present, or barcodes read by the embedded scanner of my device.我已经阅读了几篇关于如何隐藏它的文章,但没有一篇关于切换功能的文章,以便用户可以按需隐藏或显示它,并且仅使用存在的硬件键盘或我设备的嵌入式扫描仪读取的条形码。

Ok, in the end nobody replied to me so I've found a workaround.好的,最后没有人回复我,所以我找到了解决方法。 It's not the best possibile but at least in my case solved the problem.这不是最好的可能,但至少在我的情况下解决了这个问题。 I needed to disable/enable the keyboard on demand (through a function key from a Zebra rugged device), what makes things more complicated is that this feature should work with a WebView where, programmatically, there is no easy (...or at all) way to gain access of any EditText control embedded in it.我需要按需禁用/启用键盘(通过 Zebra 坚固设备上的 function 键),让事情变得更复杂的是,这个功能应该与 WebView 一起使用,其中以编程方式,没有简单的(...或在all) 访问嵌入其中的任何 EditText 控件的方法。 The work around is not perfect nor elegant, but it works for me, so I wanted to share with others that maybe are facing similar issues.解决方法既不完美也不优雅,但它对我有用,所以我想与可能面临类似问题的其他人分享。 First of all we must detect touches inside the WebView (in KOTLIN):首先,我们必须检测 WebView(在 KOTLIN 中)内部的触摸:

       webView.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, m: MotionEvent): Boolean {

                if (!keybEnabled) {

                    hideKeyboard()
                }

                return false
            }
        })

Then I added a scheduled task to hide the keyboard:然后我添加了一个计划任务来隐藏键盘:

    private fun hideKeyboard() {

        Handler().postDelayed({

            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(webView.getWindowToken(), 0)

        }, 200)
    }

The method hideKeyboard() will forcibly hide the keyboard until the user hits a button to enable it (flag keybEnabled == true). hideKeyboard() 方法将强制隐藏键盘,直到用户点击按钮启用它(标志 keybEnabled == true)。

There are 2 drawbacks with this solution.这个解决方案有两个缺点。 1) If you reduce the delay to a value close to 100 ms it is possibile that the soft keyboard is hidden before Android is starting the task to display it, so it won't work. 1) 如果您将延迟降低到接近 100 毫秒的值,则软键盘可能在 Android 开始显示它之前被隐藏,所以它不会工作。 200 ms as in the example is enough for my situation, but it depends on the device's performance.示例中的 200 毫秒对于我的情况来说已经足够了,但这取决于设备的性能。 2) Sometimes the soft keyboard appears on the screen for a few milliseconds before disappearing (only when the user taps on an EditText). 2) 有时软键盘会在屏幕上出现几毫秒后消失(仅当用户点击 EditText 时)。

If point 2 is not a problem for you, this solution should work fine.如果第 2 点对您来说不是问题,那么此解决方案应该可以正常工作。 Hope it will help others.希望它会帮助别人。

UPDATE更新

I've found a slightly different solution that solves also the keyboard appearance described above.我找到了一个稍微不同的解决方案,它也解决了上面描述的键盘外观。 It is at this link Handler Solution .它位于此链接Handler Solution

FINAL CONSIDERATIONS最后的考虑

I think that soft keyboard management in Android is horrible, it is unbelievable that after so many years it is still based on such a cumbersome logic.我觉得Android里面的软键盘管理太恐怖了,想不到这么多年还是基于这么繁琐的逻辑。 There isn't a function to simply disable/enable the soft keyboard programmatically and for all the current activity's controls, it's even worse that there isn't any SDK function that returns the soft keyboard status (displayed/hidden). There isn't a function to simply disable/enable the soft keyboard programmatically and for all the current activity's controls, it's even worse that there isn't any SDK function that returns the soft keyboard status (displayed/hidden). I think that is simply unacceptable because I do not think that Android development team would spend more the 2 hours to make available such useful features.我认为这根本无法接受,因为我不认为 Android 开发团队会花费更多的 2 个小时来提供这些有用的功能。

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

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