标签:
package cn.itcast.design.note5.webview; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import cn.itcast.design.R; public class MyWebViewActivity extends Activity { public void load(View view) { // ③ 指定 地址 // webView.loadUrl(访问地址); // webView.loadUrl("file:///android_asset/html/pritection_help.html"); // webView.loadUrl("http://192.168.18.23:8080/web/html/protection_help.html"); // ④ 传入 源代码字符串 // webView.loadData(源代码字符串, 网络媒体类型 image/png image/jeg, 编码); // try { // InputStream input = getAssets().open("help.html"); // byte[] data = StreamUtil.readInputStream(input); // webView.loadData(new String(data, "UTF-8"), "text/html", "UTF-8"); // } catch (Exception e) { // e.printStackTrace(); // } // 实时获取进度载入变化的辅助类 WebChromeClient client1 = new WebChromeClient() { // 回调函数:条件 进度发生改变的时候调用 newProgress 最新进度 @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); System.out.println("onProgressChanged " + newProgress); if (newProgress == 100) { progress.setVisibility(View.GONE); } else { progress.setVisibility(View.VISIBLE); progress.setProgress(newProgress); } } }; webView.setWebChromeClient(client1); // 辅助类二 WebViewClient WebViewClient client2 = new WebViewClient() { // 回调函数:条件 0 @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); dialog = new ProgressDialog(MyWebViewActivity.this); dialog.setTitle("传智浏览器"); dialog.setMessage("正在拼命加载中..."); dialog.show(); } // 回调函数:条件 100 @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); dialog.dismiss(); dialog = null; } }; webView.setWebViewClient(client2); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBuiltInZoomControls(true); webView.loadUrl("http://192.168.18.23:8080/"); // ① 布局progressBar 100 0--100 // ② 获取进度值 载入多少显示多少 // progress.setProgress(progress) } private ProgressDialog dialog = null; private WebView webView = null; private ProgressBar progress; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // ① 创建 Activity // ② 布局Layout --WebView setContentView(R.layout.activity_webview); webView = (WebView) findViewById(R.id.webview); progress = (ProgressBar) findViewById(R.id.progress); } }
标签:
原文地址:http://blog.csdn.net/cuishiying/article/details/51341018