gpt4 book ai didi

Android: WebView 触摸事件监听器

转载 作者:行者123 更新时间:2023-11-29 23:40:05 25 4
gpt4 key购买 nike

是否可以在我在 webview 上定义的屏幕的某些部分启用/禁用触摸事件?

最佳答案

可以在您的网页中通过javaScript定义您的 Action ,然后在您的android代码中触发一个java方法

引用here定义如下所示的接口(interface):

/** Instantiate the interface and set the context  */
class WebAppInterface(private val mContext: Context) {

/** Show a toast from the web page */
@JavascriptInterface
fun showToast(toast: String) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show()
}
}

然后附加到webView

val webView: WebView = findViewById(R.id.webview)
webView.addJavascriptInterface(WebAppInterface(this), "Android")

你应该在你的网页中实现如下所示的功能,并在需要时触发它

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>

关于Android: WebView 触摸事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51948121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com