控件WebView的使用
来源:互联网 发布:怎么没人去淘宝打假 编辑:程序博客网 时间:2024/06/06 03:28
1、概述
Android WebView在Android平台上是一个特殊的View, 他能用来显示网页,这个类可以被用来在你的app中仅仅显示一张在线的网页,还可以用来开发浏览器。WebView内部实现是采用渲染引擎来展示view的内容,提供网页前进后退,网页放大,缩小,搜索。在Android 4.3系统及其一下WebView内部采用Webkit渲染引擎,在Android 4.4采用chromium 渲染引擎来渲染View的内容。
A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.
2、使用
使用时,主要注意两个方法:
setWebViewClient: 主要处理解析,渲染网页等浏览器做的事情
setWebChromeClient: 辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等
WebViewClient就是帮助WebView处理各种通知、请求事件的。
具体使用:
在AndroidManifest.xml设置访问网络权限:
<span style="font-size:18px;"><uses-permission android:name="android.permission.INTERNET"/></span>
控件:
<span style="font-size:18px;"><WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" /></span>
a.加载本地html资源/web资源,
本地,
资源放在assets文件夹下:
webView = (WebView) findViewById(R.id.webView);webView.loadUrl("file:///android_asset/example.html");
网络,
webView = (WebView) findViewById(R.id.webView);webView.loadUrl("http://baidu.com");
b、设置webViewClient
需要重写自己的WebViewClient,通过setWebViewClient关联
package com.example.testopen;import android.app.Activity;import android.os.Bundle;import android.webkit.WebView;import android.webkit.WebViewClient;public class MainActivity extends Activity {private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); init(); } private void init(){ webView = (WebView) findViewById(R.id.webView); //WebView加载web资源 webView.loadUrl("http://baidu.com"); //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url);//用对应webView加载对应url内容进行显示 return true; } }); } }
c、设置js相关
如果访问的页面中有Javascript,则webview必须设置支持Javascript
//启用支持javascriptWebSettings settings = webView.getSettings();settings.setJavaScriptEnabled(true);
添加js交互:
webView.addJavascriptInterface(new Object() {<span style="white-space:pre"></span>// 打开照相机@JavascriptInterfacepublic void openCamera(String path) {pathAll = Tool.getPath(context) + "app_package/" + path;WebTool webTool = WebTool.getInstance();webTool.openCamera(path, activity);}}, "androidApi");
在java中处理js中的回调方法,使用的是 webView.loadUrl("javascript:locationCallBack()");
其中,locationCallBack()是js的一个方法。
另:
缓存,加载进度设置,见 http://www.cnblogs.com/tinyphp/p/3858997.html
webView回调的详情,参见
http://tool.oschina.net/uploads/apidocs/android/reference/android/webkit/WebView.html
http://blog.csdn.net/typename/article/details/39030091
- WebView控件的使用
- 控件WebView的使用
- 控件WebView的使用
- WebView控件的使用
- WebView控件的使用
- Webview控件的使用
- webview控件的使用
- android Android:控件WebView显示网页webview的使用
- Android中WebView控件的使用
- Android中WebView控件的基本使用
- Android开发---WebView控件的使用
- [android进阶]webview控件的使用
- android控件:WebView的初使用
- Android-控件WebView的基本使用
- webView控件基本使用
- 学习webView控件使用
- WebView控件使用
- WebView 控件使用Demo
- 第2周 项目0 - 宣告“主权”
- 什么是RichFaces
- ThinkPHP通过AJAX返回JSON的两种实现方法
- PostgreSQL分区表(Table Partitioning)
- django的session操作
- 控件WebView的使用
- 沙盒文件管理
- 控件测试
- android 百度地图 点击选中到最上层 置顶
- Understanding bidirectional relationship from Hibernate documentation
- 宣告“主权”
- 验证码-技术选型
- Android studio快捷键设置为Eclipse风格
- Linux2.6.32移植到MINI2440(3)支持yaffs2文件系统