我想单击 Android Web 视图中的术语来执行操作,例如显示包含我们已单击的术语的吐司。
`class WebAppInterface(private val context: Context) {
@JavascriptInterface
fun showToast(toastMessage: String) {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
}
}
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun HtmlText(htmlContent: String) {
val rightAlignedHtmlContent = """
<html>
<head>
<style>
body {
text-align: right;
}
</style>
</head>
<body>
$htmlContent
</body>
</html>
""".trimIndent()
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Transparent, shape = RoundedCornerShape(16.dp))
.border(width = 1.dp, color = Color.Transparent),
) {
AndroidView(
modifier = Modifier
.fillMaxSize().padding(8.dp)
.background(color = Color.Transparent, shape = RoundedCornerShape(16.dp))
.border(width = 1.dp, color = Color.Transparent),
factory = { context ->
WebView(context).apply {
webViewClient = WebViewClient()
settings.javaScriptEnabled = true
addJavascriptInterface(WebAppInterface(context), "Android")
loadDataWithBaseURL(null, rightAlignedHtmlContent, "text/html", "UTF-8", null)
}
},
update = { webView ->
webView.loadDataWithBaseURL(null, rightAlignedHtmlContent, "text/html", "UTF-8", null)
}
)
}
}`
这是我的jetpack 撰写代码。
AndroidView 中每个术语的点击不起作用。
你解决了吗?我找不到使用 WebMessageListener 的示例