2 回答
TA贡献1831条经验 获得超10个赞
创建自定义 webview,覆盖 onCreateInputConnection 以设置输入时间选项和键盘输入类型,覆盖 dispatchKeyEvent 以获取关键事件将其过滤掉
例子 :
class MyWeb@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val inputConnection = BaseInputConnection(this, false)
return inputConnection
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
super.dispatchKeyEvent(event)
val dispatchFirst = super.dispatchKeyEvent(event)
if (event.action == KeyEvent.ACTION_UP) {
when (event.keyCode) {
KeyEvent.KEYCODE_ENTER -> {
Toast.makeText(context,"Hii",Toast.LENGTH_LONG).show()
//callback?.onEnter()
}
}
}
return dispatchFirst
}
}
和XML
<com.example.MyWeb
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/web"
/>`
资料来源:https ://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106
TA贡献1809条经验 获得超8个赞
按键事件几乎从不从软键盘发送,它们使用更直接的方法。
Android 键盘的工作方式是绑定到视图。该视图必须实现 getInputConnection() 返回一个对象,该对象将允许键盘应用程序调用(通过 AIDL)函数。这些功能之一称为“操作键”(完成按钮)。在默认的 InputConnection 实现中,这将调用注册到绑定视图的侦听器。
由于您在这里处理网络视图 - 我认为没有办法直接捕获它。您可以尝试将 WebView 子类化为 ActionKeyWebView。添加一个函数来注册一个动作键监听器接口。覆盖 getInputConnection 以返回您自己的 InputConnectionWrapper 子类,并包装 super.getInputConnection()。然后覆盖 performEditorAction 以调用为 webview 注册的任何侦听器。它有相当多的代码,但它应该可以工作。
添加回答
举报