【发布时间】:2019-10-21 08:39:28
【问题描述】:
是否可以检测到在 Android WebView 中加载的网页上的按钮点击?单击特定按钮时,我试图显示图像。有什么方法可以实现吗?
【问题讨论】:
-
它不工作
是否可以检测到在 Android WebView 中加载的网页上的按钮点击?单击特定按钮时,我试图显示图像。有什么方法可以实现吗?
【问题讨论】:
您可以使用 JavascriptInterface 注解与 webview 中的任何网页进行交互。
定义一个带有 JavascriptInterface 注释的方法
公共类 WebAppInterface { 上下文 mContext;
// Instantiate the interface and set the context
WebAppInterface(Context c) {
mContext = c;
}
// Show a toast from the web page
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
并将此类的实例添加到 webview
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");
并从网页中的javascript调用方法。
//这是一个javascript代码
AndroidInterface.showToast('Hello');
【讨论】: