【问题标题】:How to detect button click on webpage in android webview如何在android webview中检测按钮点击网页
【发布时间】:2019-10-21 08:39:28
【问题描述】:

是否可以检测到在 Android WebView 中加载的网页上的按钮点击?单击特定按钮时,我试图显示图像。有什么方法可以实现吗?

【问题讨论】:

标签: android android-webview


【解决方案1】:

您可以使用 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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2021-07-10
    • 2016-12-24
    • 2021-12-12
    • 2012-01-28
    相关资源
    最近更新 更多