我在我的安卓应用程序中使用了WebView
。我想在用户单击按钮时运行Push Notification
。谁能帮我提供java代码来处理WebView
的onClick
按钮。
您可以使用JavascriptInterface
在Android
中实现此目的
在你的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
单击调用onButtonClick()
方法
下面是代码
<html>
<body>
<a onClick="onButtonClick()"> Click me, i am JS Button </a>
</body>
</html>
这将调用活动onButtonClick()
方法。
希望这将有助于您检查此链接的更多澄清