您的位置:首页 > 移动开发

【Android】获取webview文章的标题

2017-09-10 16:23 465 查看
应用开发中需要获取WebView当前页面的标题,方法如下:

 WebChromeClient wvcc = new WebChromeClient() {  
            @Override  
            public void onReceivedTitle(WebView view, String title) {  
                super.onReceivedTitle(view, title);  
                txtTitle.setText("ReceivedTitle:" +title);  
            }  
  
        }; 

附webview代码参考作为备忘记录:

import android.annotation.SuppressLint;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.CookieManager;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class H5Activity extends BaseActivity implements OnClickListener {

    private WebView contentWebView;

    private RelativeLayout loading_container, error_container;

    private String url;

    private String title;

    private WebSettings webSettings;

    private TextView titleText;

    

    private String mID;

    private String LogonUrl = "";

 

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_web);

        initData();

        initView();

    }

    private void initData() {

        Intent intent = getIntent();

        title = intent.getStringExtra(Constant.TITLE);

        url = intent.getStringExtra(Constant.JUMP_URL);

    }

    @SuppressLint("SetJavaScriptEnabled")

    private void initView() {

        initHeaderView();

        contentWebView = (WebView) findViewById(webview);

        // 启用javascript

        contentWebView.getSettings().setJavaScriptEnabled(true);

        

        String ua = contentWebView.getSettings().getUserAgentString();

        contentWebView.getSettings().setUserAgentString(ua + " HRDAdroidApp");

        contentWebView.loadUrl(url);

        loading_container = (RelativeLayout) findViewById(R.id.loading_container);

        error_container = (RelativeLayout) findViewById(R.id.error_container);

        contentWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        //对WebView进行各种设置

        webSettings = contentWebView.getSettings();

        // 设置WebView属性,能够执行Javascript脚本

        webSettings.setJavaScriptEnabled(true);

        // 设置出现缩放工具

        webSettings.setBuiltInZoomControls(false);

        //优先使用缓存

        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        //不使用缓存

        //webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        //打开页面时, 自适应屏幕:

        webSettings.setUseWideViewPort(true);//设置此属性,可任意比例缩放//将图片调整到适合webview的大小

        webSettings.setLoadWithOverviewMode(true);// 缩放至屏幕的大小

        //支持缩放

        webSettings.setBuiltInZoomControls(false);//设置支持缩放

        webSettings.setSupportZoom(false); //支持缩放

        //如果webView中需要用户手动输入用户名、密码或其他,则webview必须设置支持获取手势焦点。

        contentWebView.requestFocusFromTouch();

        contentWebView.setVerticalScrollBarEnabled(false);

        contentWebView.setHorizontalScrollBarEnabled(false);

        webSettings.setLoadsImagesAutomatically(true);  //设置自动加载图片

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗

        webSettings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局

        webSettings.supportMultipleWindows();//多窗口

        webSettings.setAllowFileAccess(true);  //设置可以访问文件

        webSettings.setDomStorageEnabled(true);

        webSettings.setDatabaseEnabled(true);

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口

        webSettings.setLoadsImagesAutomatically(true);  //支持自动加载图片

        // 此处能拦截超链接的url,即拦截href请求的内容.

        contentWebView.setWebChromeClient(new WebChromeClient() {

            @Override

            public void onReceivedTitle(WebView view, String title) {

                super.onReceivedTitle(view, title);

                titleText.setText(title);

            }

            @Override

            public void onProgressChanged(WebView view, int progress) {

                //txtTitle.setText("");

               /* loadingProgress.setVisibility(View.VISIBLE);

                loadingProgress.setProgress(progress * 100);

                if (progress == 100) {

                    loadingProgress.setVisibility(View.GONE);

                    txtTitle.setText(mytitle);

                }*/

                super.onProgressChanged(view, progress);

            }

        });

        contentWebView.setWebViewClient(new WebViewClient() {

            //在点击请求的是链接是才会调用,重写此方法返回true表明点击网页里面的链接还是在当前的webview里跳转,

            //不跳到浏览器那边。这个函数我们可以做很多操作,比如我们读取到某些特殊的URL,

            //于是就可以不打开地址,取消这个操作,进行预先定义的其他操作,这对一个程序是非常必要的。

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                Log.e("VVV", "shouldOverrideUrlLoading: "+url);

                view.loadUrl(url);

                //获取cookies

                CookieManager cm = CookieManager.getInstance();

                String cookies = cm.getCookie(url);

                //SPUtils.putString(MainActivity.this,"cook",cookies);

                return true;

            }

            @Override

            public void onPageFinished(WebView view, String url) {

                // view.getSettings().setJavaScriptEnabled(true);

                super.onPageFinished(view, url);

                // html加载完成之后,添加监听图片的点击js函数

                // addImageClickListner();

                error_container.setVisibility(View.INVISIBLE);

                loading_container.setVisibility(View.INVISIBLE);

            }

            @Override

            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                // view.getSettings().setJavaScriptEnabled(true);

                super.onPageStarted(view, url, favicon);

            }

            @Override

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                loading_container.setVisibility(View.INVISIBLE);

                error_container.setVisibility(View.VISIBLE);

                super.onReceivedError(view, errorCode, description, failingUrl);

            }

        });

    }

    private void initHeaderView() {

        RelativeLayout btnleft = (RelativeLayout) findViewById(R.id.back_layout);

        btnleft.setVisibility(View.VISIBLE);

        titleText = (TextView) findViewById(R.id.tv_title_bar_text);

        //titleText.setText(title);

        RelativeLayout btnright = (RelativeLayout)findViewById(R.id.right_layout);

        btnright.setVisibility(View.INVISIBLE);

        

    }

    public void onBack(View v) {

        Intent resultIntent = new Intent();

        H5Activity.this.setResult(RESULT_CANCELED, resultIntent);

        finish();

    }

    @Override

    public void onClick(View view) {

        switch (view.getId()) {

        default:

            break;

        }

    }

 

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK && contentWebView.canGoBack()) {

            contentWebView.goBack();// 返回前一个页面

            return true;

        }

        return super.onKeyDown(keyCode, event);

    }

}

知行办公,专业移动办公平台 https://zx.naton.cn/
原创团队

【总监】十二春秋之,3483099@qq.com;

【Master】zelo,616701261@qq.com;【运营】运维艄公,897221533@qq.com;

【产品设计】流浪猫,364994559@qq.com;【体验设计】兜兜,2435632247@qq.com;

【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;

【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;

【java】首席工程师MR_W,feixue300@qq.com;【测试】土镜问道,847071279@qq.com

【数据】fox009521,42151960@qq.com;【安全】保密,你懂的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: