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 注冊的任何偵聽器。它有相當多的代碼,但它應該可以工作。
添加回答
舉報