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



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

您可以使用JavascriptInterfaceAndroid中实现此目的

在你的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中,您必须WebPageButton单击调用onButtonClick()方法

下面是代码

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

这将调用活动onButtonClick()方法。

希望这将有助于您检查此链接的更多澄清

最新更新