Android WebView 从 WebViewClient 到 Activity 的通信

标签 android webviewclient

我有一个简单的 helloworld 应用程序。我试图告诉 Activity 用户点击了“Cnn.com”

    WebViewClient wc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("http://cnn.com/")) {
                //TELL ACTIVITY CNN WAS CLICKED
                return true;
            } else {
                return false;

            }
        }

    };

    mWebView.setWebViewClient(wc);

我该怎么做。

(我来自 C# .NET 背景)

最佳答案

public class YourActivity extends Activity{

    // bla bla bla

    // the code you already have
    WebViewClient wc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("http://cnn.com/")) {
                YourActivity.this.tellMe();
                return true;
            } else {
                return false;

            }
        }

    };

    // this is the method to 'tell' the activity that someone clicked the link
    public void tellMe(){
        // in this case I just raise a toast.
        // but you can do whatever you want here ;)
        Toast.makeText(YourActivity.this, "eyy!! somebody clicked the cnn link", 1).show();
    }
}

关于Android WebView 从 WebViewClient 到 Activity 的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250539/

相关文章:

java - Android: R.java: 错误 <identifier> 预期

java - 传递 3 个元素( Intent )

android - 是否可以在 onCreateWindow 创建的 WebView 上调用 addJavascriptInterface?

android - 没有可用的互联网连接时如何添加自定义错误页面

android - Webview - 在显示网站之前更改页面源?

android - Flutter项目添加华为agconnect后构建失败

java - 在获取应用程序数据使用时满足运行 Jelly bean 以及 Jelly bean 之后版本的设备

android - android webview 中的后退按钮?

java - Android 请求在 webview 中使用位置的权限

android - 管理android应用程序中的多个 Activity