WebView个人总结

1、添加上网权限:<uses-permission android:name="android.permission.INTERNET" />

2、设置webview

 1 WebView webView;
 2 WebSettings ws;
 3 
 4 
 5 ws = webView.getSettings();
 6 ws.setAppCacheEnabled(true);// 设置启动缓存
 7 ws.setAppCacheMaxSize(1024 * 10);// 设置最大缓存
 8 ws.setSupportZoom(true);// 设置成拖动放大缩小
 9 ws.setBuiltInZoomControls(true);
10 ws.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置缓存模式
11 // 设置支持Javascript
12 webView.getSettings().setJavaScriptEnabled(true);
13 webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
14 
15 // 缓存,离线应用
16 webView.getSettings().setAppCacheEnabled(true);
17 webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
18 webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

3、获取网页对话框

webView.setWebChromeClient(new WebChromeClient() {

@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
// TODO Auto-generated method stub
// 构建一个Builder来显示网页中的对话框
Builder builder = new Builder(context);
builder.setTitle("警告");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// 点击确定按钮之后,继续执行网页中的操作
result.confirm();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
}

@Override
public boolean onJsConfirm(WebView view, String url,
String message, final JsResult result) {
// TODO Auto-generated method stub
Builder builder = new Builder(context);
builder.setTitle("confirm");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
});
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.cancel();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
}

@Override
public void onProgressChanged(WebView view, int newProgress) {// 加载进度
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
}

});

4、如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。
  给WebView添加一个事件监听对象(WebViewClient)

5、webview取得焦点

webView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

webView.requestFocus();
return false;
}
});

6、提示加载对话框

pd=ProgressDialog.show(context, "请稍后", "正在加载中...");

  当网页加载完成后,调用pd.dismiss();

posted on 2012-05-31 08:41  月vs枫  阅读(4338)  评论(2编辑  收藏  举报