WebView的使用

          
  webView= (WebView) findViewById(R.id.webview);
        titleTV= (TextView) findViewById(R.id.title);
        iconIV= (ImageView) findViewById(R.id.icon);
        webView.loadUrl(url);
        WebSettings webSettings= webView.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持js
        webSettings.setSupportZoom(true);//支持缩放
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        dialog=new Dialog(this);
        dialog.setTitle("你点击了屏幕");
        webView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用js方法,传递参数
                //webView.loadUrl("javascript:invokedByJava('android')");
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {

                } else {

                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                titleTV.setText(title);
            }

            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                iconIV.setImageBitmap(icon);
            }
        });
        //阻止webView打开系统浏览器
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }
        });
        //监听网页中的下载
        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                if (TextUtils.isEmpty(url))
                    return;
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        //webView调用js
        webView.loadUrl("javascript:do()");
        //js调用webView
        webView.addJavascriptInterface(new JavascriptInterface(),"demo");

你可能感兴趣的:(android,webView)