android.webkit.WebView/WebViewClient/WebChromeClient
使用android.webkit.WebView控件
在xml布局文件中定义
<WebView
android:id=”@+id/webkit01”
android:layout:width=”fill_parent”
android:layout:height=”fill_parent”
android:layout:weight=”1” />
在程序中使用WebView
mWebView = (WebView) findViewById(R.id.webview01);
mWebView.loadUrl(“http://www.google.com”);
通过WebSettings来设置WebView的属性和状态
WebSettings webSettings = mWebview.getSettings();
当WebView销毁后,再使用WebSettings会抛出IllegalStateException异常。
WebSettings常用方法
1. setAllowFileAccess, 启用/禁止WebKit访问文件数据
2. setBlockNetworkImage, 是否显示网络图像
3. setBuiltInZoomControls, 是否支持缩放
4. setCacheMode, 设置缓存模式
5. setDefaultFontSize, 设置默认字体大小
6. setDefaultTextEncodingName, 设置默认的解码方式
7. setFixedFontFamily, 设置固定使用的字体
8. setJavaScriptEnabled, 是否支持JavaScript
9. setLayoutAlgorithm, 设置布局方式
10. setLightTouchEnabled,
11. setSupportZoom, 是否支持变焦
使用WebViewClient
WebViewClient用来处理各种通知、请求等事件,WebView调用setWebViewClient()来指定一个WebViewClient对象。
WebViewClient常用方法
1. doUpdateVisitedHistory, 更新历史记录
2. onFormResubmission, 重新请求网页数据
3. onLoadResource, 加载资源
4. onPageFinished, 网页加载完毕
5. onPageStarted, 网页开始加载
6. onReceivedError, 报告错误信息
7. onScaleChanged, 发生Scale改变
8. shouldOverrideUrlLoading, 控制新的连接在当前WebView中打开
使用WebChromeClient
WebChromeClient用来处理JavaScript对话框、网站图标、网站title、加载进度等。
WebChromeClient常用方法
1. onCloseWindow,
2. onCreateWindow,
3. onJsAlert,
4. onJsConfirm,
5. onJsPrompt,
6. onProgressChanged,
7. onReceivedIcon,
8. onReceivedTitle,
9. onRequestFocus
示例:实现简单浏览网页的功能
// 点击返回键返回到前一个页面
if((keyCode==KeyEvent.KEYCODE_BACK) && (mWebView.canGoBack()))
mWebView.goBack();
// 学习如何处理JavaScript常用对话框
WetSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setBuiltInZoomControls(true);
// 设置WebViewClient
mWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// onPageFinished
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
// onPageStarted
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
// 设置WebChromeClient
mWebView.setWebChromeClient(new WebChromeClient(){
// 处理JavaScript中的alert
public boolean onJsAlert(WebView view, String url, String msg, fianl JsResult result) {
// 弹出对话框 builder.show()
// 确定
result.confirm();
return true;
}
// 处理JavaScript中的confirm
public boolean onJsConfirm(WebView view, String url, String msg, fianl JsResult result) {
// 弹出对话框 builder.show()
// 确定
result.confirm();
// 否定
result.cancel();
return true;
}
// 处理JavaScript中的prompt
public boolean onJsPrompt(WebView view, String url, String msg, String defaultValue, fianl JsPromptResult result) {
// 弹出对话框 builder.show()
// 确定
result.confirm(value);
// 否定/取消
result.cancel();
return true;
}
// 处理网页加载进度条
public void onProgressChanged(WebView view, int newProgress) {
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
super.onProgressChanged(view, newProgress);
}
// 得到网页的标题,设置app的标题title。
public void onReceivedTitle(WebView view, String title) {
setTitle(title);
super.onReceivedTitle(view, title);
}
});
// 连接按钮点击事件处理程序
String url = mUrlBox.getText().toString();
if(URLUtil.isNetworkUrl(url)){
mWebView.loadUrl(url);
}
// 点击返回按键
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
// 示例代码:弹出对话框
final View dialogView = mInflater.inflate(R.layout.xxx, null);
Builder builder = new Builder(mContext);
builder.setTitle(“xxx”);
builder.setView(dialogView);
// 肯定
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnclickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
// 否定
builder.setNegativeButton(android.R.string.cancel, new AlertDialog.OnclickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
// 取消
builder.setOnCancelListener(new AlertDialog.OnCancelListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
// 不允许取消
builder.setCancelable(false);
builder.create();
builder.show();
return true;
android.webkit.WebView/WebViewClient/WebChromeClient的更多相关文章
- "android.uid.systemandroid.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
在android源码中编译app通过,运行时出现错误: "android.uid.systemandroid.view.InflateException: Binary XML file l ...
- WebView WebViewClient WebChromeClient
在android中,浏览器的功能分成几个部分,每个部分分工明确,互相协作.其中: 1. WebView :专门负责网页数据解析和渲染: 2. WebViewClient :帮助WebView处理各种请 ...
- Android之WebView的使用样例——WebSetting、WebViewClient、WebChromeClient
点击查看原文 代码直接下载http://download.csdn.net/detail/metis100/8514837 第一步,xml Manifest中要设置网络权限,否则会出先 webpage ...
- android使用Webview上传图片
package com.example.webview; import java.io.File; import android.net.Uri;import android.os.Bundle;im ...
- Android之 -WebView实现离线缓存阅读
前言 本篇博客要实现的是一个离线下载和离线阅读的功能,这是很多阅读类app都常见的一个功能,典型的应用就是网易新闻.什么是离线下载?其实这个概念是比较模糊,是离线之后下载呢,还是下载之后离线,但稍微有 ...
- Android:WebView(慕课网)
使用webview最重要的三点: 1 WebView加载本地资源(webView.loadUrl("file:///android_asset/example.html");) 2 ...
- Android之webview详解
文章大纲 一.webview基本介绍1.什么是webview2.为什么要使用webview3.webview基本操作 二.webview高级使用1.WebView状态2.资源加载3.WebView加载 ...
- Android 使用WebView显示网页
构建WebView就可以显示Web信息.因为我觉得这里会讲述很多方式来实现WebView,所以我决定为每一种方式创建一个对应的Activity,MainActivity通过Button可以点击进入对应 ...
- Android Internet - WebView 的使用
WebView是Android 提供的操作网页的一个组件. 用于浏览网页及其它Internet资源. 这里总结了一些WebView 的经常使用接口.和2个小演示样例程序用于自己开发时直接使用.就不用再 ...
随机推荐
- pytest文档27-pytest分布式执行(pytest-xdist)
前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...
- java继承时候类的运行顺序问题
子类在继承父类后,创建子类对象会首先调用父类的构造函数,先运行父类的构造函数,然后再运行子类的构造函数,例如以下所看到的: class Father{ public Father(){ System. ...
- java、js中实现无限层级的树形结构(类似递归)
js中: var zNodes=[ {id:0,pId:-1,name:"Aaaa"}, {id:1,pId:0,name:"A"}, {id:11,pId:1 ...
- git 经常使用操作集锦
创建仓库 新建普通仓库: jxdong@ubuntu-server:~/workspace/git$ git init Reinitialized existing Git repository in ...
- 服务信息块协议 SMB(Server Message Block protocol)
SMB(Server Message Block)是协议名,它能被用于Web连接和客户端与服务器之间的信息沟通. SMB协议 SMB最初是IBM的贝瑞·费根鲍姆(Barry Feigenbaum)研制 ...
- DP 换硬币问题
设有n种不同面值的硬币,现要用这些面值的硬币来找开待凑钱数m,可以使用的各种面值的硬币个数不限. 找出最少需要的硬币个数,并输出其币值. package DP; import java.util ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- iframe跨域与session失效问题
何为跨域跨域session/cookie? 也就是第三方session/cookie.第一方session/cookie指的是访客当前访问的网站给访客的浏览器设置的seesion /cookie, 会 ...
- NYOJ-61 传纸条(一)
传纸条(一) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...
- Android -- 保存文件
背景 我们以常见 ...