Android WebView 获取网页title

作者: android 发布时间: 2019-08-21 浏览: 4919 次 编辑

Android WebView 获取网页的title,可以通过两种方式获取

方法一 、 onPageFinished函数

//如果不设置WebViewClient,请求会跳转系统浏览器
webView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;//super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (TextUtils.isEmpty(title)) {
            setTitle(view.getTitle());
        }
    }
});

方法一 、 onReceivedTitle函数

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if (title != null) {
          titleView.setCenterText(title);
        }
    }
});