Android中WebView的点击事件无法获取当前URL

发布于 2024-11-26 19:20:43 字数 627 浏览 2 评论 0原文

    private WebView wv;
    public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.infoweblinkview);

    wv=(WebView) findViewById(R.id.weblinkview);
    wv.setWebViewClient(new WebViewClient());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(getString(R.string.infourl));


 wv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String  previousUrl = wv.getUrl();
                }
            });

}
    private WebView wv;
    public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.infoweblinkview);

    wv=(WebView) findViewById(R.id.weblinkview);
    wv.setWebViewClient(new WebViewClient());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(getString(R.string.infourl));


 wv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String  previousUrl = wv.getUrl();
                }
            });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酒忠诚 2024-12-03 19:20:43

为此,您可以重写 WebViewClient 类中的某些方法:

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    return super.shouldOverrideUrlLoading(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    // TODO Auto-generated method stub
    super.onPageStarted(view, url, favicon);
}

根据您的要求使用此方法中的任何一个,String url 为您提供 url。

For that, your can override some method in your WebViewClient class :

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    return super.shouldOverrideUrlLoading(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    // TODO Auto-generated method stub
    super.onPageStarted(view, url, favicon);
}

Use any of this method as per your requirement, the String url gives you the url.

吹梦到西洲 2024-12-03 19:20:43

仅当当前 webview 即将加载新 URL 时,才会调用 shouldOverrideUrlLoading() 方法。因此,要在单个网页应用程序内单击时接收更新的 URL,您需要扩展 WebChromeClient 类并将其实例设置为您的 webview。每当 URL 更新时 onProgressChanged 都会被调用,你将通过 webview 获得更新的 URL。

webView?.webChromeClient = QuartzWebChromeClient() 

QuartzWebChromeClient 的 onProgressChanged() 内部可以通过 webview 接收更新的 URL。

 private inner class QuartzWebChromeClient : WebChromeClient() {
    override fun onProgressChanged(view: WebView, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        Log.d("tag",view.url)
    }
}

The method shouldOverrideUrlLoading() will be called only when a new URL is about to load in the current webview. Hence to receive an updated URL upon click inside a single webpage application you need to extend WebChromeClient class and set its instance to your webview. Whenever URL will update onProgressChanged will be called and there you will get updated URL through webview.

webView?.webChromeClient = QuartzWebChromeClient() 

Inside onProgressChanged() of QuartzWebChromeClient updated URL can be received through webview.

 private inner class QuartzWebChromeClient : WebChromeClient() {
    override fun onProgressChanged(view: WebView, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        Log.d("tag",view.url)
    }
}
安静被遗忘 2024-12-03 19:20:43

就像@Fatal Exception所说,这是一个不错的选择:

_browserWebview?.webChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            println("URL WEBVIEW: ${view.url}")
        }
    }

Like @Fatal Exception says, it´s a good option:

_browserWebview?.webChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            println("URL WEBVIEW: ${view.url}")
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文