如何在Android WebView中加载网页标题?

时间:2011-11-19 09:56:59

标签: android android-webview

我在我的应用程序中使用WebView,我需要根据用户所在的页面更改应用程序标题。如何在Android WebView中执行此操作?

我是通过以下行在iphone中完成此操作

self.title = [webPage stringByEvaluatingJavaScriptFromString:@"document.title"]

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Adds Progrss bar Support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.webview );

        // Makes Progress bar Visible
        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

        mWebView = (WebView) findViewById( R.id.webview ); //This is the id you gave 
        //to the WebView in the main.xml
        mWebView.getSettings().setJavaScriptEnabled(true);   
        mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
        //if ROM supports Multi-Touch      
        mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM

        // Load URL
        Bundle b = getIntent().getExtras();
        String url = b.getString("url");
        Log.d(TAG, "url " + url);
        mWebView.loadUrl(url);


        // Sets the Chrome Client, and defines the onProgressChanged
        // This makes the Progress bar be updated.
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress){
                //Make the bar disappear after URL is loaded, and changes string to Loading...
                myActivity.setTitle("Loading...");
                myActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

                // Return the app name after finish loading
                if(progress == 100)
                    myActivity.setTitle(R.string.app_name);
            }
        });

        mWebView.setWebViewClient(new WebViewClient(){

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

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

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ImageView logoImageView = (ImageView)findViewById(R.id.logoimage);
                logoImageView.setVisibility(View.GONE);
                Log.d(TAG, "view.getTitle() " + view.getTitle());
                myActivity.setTitle(view.getTitle());
            }

        });

    }

7 个答案:

答案 0 :(得分:84)

您必须使用自定义WebViewClient才能完成此操作。 您将覆盖onPageFinished()方法,以便在新页面加载完成后,您可以将webview设置为适当的标题。

以下是上述示例实现:

    WebView mWebView = (WebView) findViewById(R.id.mwebview);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            ExperimentingActivity.this.setTitle(view.getTitle());
        }
    });

在初始化网页视图时,您将会这样做。 将“ExperimentingActivity”替换为您的活动名称。

如果您已经覆盖了WebViewClient,只需将此函数或代码添加到您现有的函数中。

您可以在此处获取有关我正在使用的类和函数的更多信息:

Android Developers: Activity - setTitle()

Android Developers: WebViewClient

Android Developers: WebView

答案 1 :(得分:44)

我的观察是,您使用WebChromeClient获取网页标题的时间比使用WebViewClient

的时间短
webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if (!TextUtils.isEmpty(title)) {
            YourActivity.this.setTitle(title);
        }
    }
});

答案 2 :(得分:1)

private WebViewClient mWvClient = new WebViewClient() {

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {

            String title = view.getTitle();//getTitle
            super.doUpdateVisitedHistory(view, url, isReload);
        }
    }

答案 3 :(得分:0)

所以我的经验你应该使用onReceivedTitle和onPageFinished两者。我发现有时onPageFinished如果你使用react-router或者类似的东西,你将不会收到通知。而onReceivedTitle就像@ michael-levy提到的那样有缺陷。

答案 4 :(得分:0)

如果您不想要整个标题,只需要一个域名,如果您只想在www.google.com上显示“Google”,并且您拥有此标题中的所有查询参数,该怎么办。

    URL hostUrl= new URL(webView.getUrl());
    String title = hostUrl.getHost();
    title = title.startsWith("www.") ? title.substring(4) : title;

答案 5 :(得分:0)

在Kotlin中阅读页面标题的最简单方法:

      webView.webViewClient = object : WebViewClient() {
         //....
          override fun onPageFinished(view: WebView, url: String) {
           val title = view.title
            }
           //...
          }

答案 6 :(得分:0)

从Webview获取图像和标题

图片

img.setImageBitmap(webview1.getFavicon());

标题

txt.setText(webview1.getTitle());