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

Sam*_*Sam 42 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());
            }

        });

    }
Run Code Online (Sandbox Code Playgroud)

Dal*_*osa 89

您必须使用自定义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());
        }
    });
Run Code Online (Sandbox Code Playgroud)

在初始化webview时,您将会这样做.将"ExperimentingActivity"替换为您的活动名称.

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

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

Android开发者:Activity - setTitle()

Android开发者:WebViewClient

Android开发者:WebView


Zeb*_*eba 45

我的观察是,你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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 好的解决方案 为很多人工作过.非常感谢. (3认同)
  • 调用WebView.goBack()后似乎没有调用它.所以,如果你需要处理后退导航,我认为onPageFinished()可能是更好的选择. (2认同)
  • 请参阅 https://code.google.com/p/android/issues/detail?id=66337。看起来 onReceivedTitle() 行为在某些 Android 版本中被破坏了。 (2认同)