gpt4 book ai didi

android - 如何在 Android Java 中处理 Webview 点击事件

转载 作者:搜寻专家 更新时间:2023-11-01 09:24:22 24 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 WebView。我想在用户单击按钮时运行 Push Notification。谁能帮我提供 java 代码来处理 WebViewonClick 按钮。

最佳答案

您可以在 Android 中使用 JavascriptInterface 实现此目的

在您的Activity 中创建类,将其命名为JavaScriptInterface 类。创建方法
onButtonClick() 类内部

public class JavaScriptInterface {
Context mContext;

/**
* Instantiate the interface and set the context
*/
JavaScriptInterface(Context c) {
mContext = c;
}


@JavascriptInterface
public void onButtonClick() {
// Handle your code

}

}

将此类引用添加到 WebView,如下所示。

 webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");

并且在您的 WebPage 中,您必须在 WebPage Button click 中调用 onButtonClick() 方法

下面是代码

<html>
<body>
<a onClick="onButtonClick()"> Click me, i am JS Button </a>
</body>
</html>

这将调用您的 Activity onButtonClick() 方法。

希望这会有所帮助 如需更多说明,请查看此 link

关于android - 如何在 Android Java 中处理 Webview 点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908545/

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