1.Android下WebView控件定义
定义:WebView类是WebKit模块的Java层的视图类,所有需要使用Web浏览功能的Android应用程序都需要创建该视图对象显示和处理请求的网络资源。
当前,WebKit模块支持Http、Https、FTP以及Javascript请求。WebView作为应用程序的UI接口,为用户提供一系列的网页浏览、用户交互接口,客户程序通过这些接口访问WebKit核心代码。
2.Android下Webview控件浏览网页
WebView控件来源于:import android.webkit.WebView;
WebView webview=(WebView)findViewById(R.id.webview); //获取当前WebView上的对象
webview.loadUrl("http://www.google.com"); //使用loadUrl来加载指定的网页
1) WebView控件不仅可以浏览Internet上的网页,也可以浏览本地的网页文件或任何WebView支持的文件,代码如下:
----------------------------------------------------------------------------------------------------------------------
webview.loadUrl("file:///sdcard/tencentOne.jpg");
----------------------------------------------------------------------------------------------------------------------
2) 除了可以浏览网页外,WebView控件也和大多数浏览器一样,可以缓存浏览历史页面,并且使用如下代码向后和向前浏览:
---------------------------------------------------------------------------------------------------------------------
webview.goBack(); //向后浏览历史页面
webview.goForward(); //向前浏览历史页面
----------------------------------------------------------------------------------------------------------------------
3)想清除缓存内容:
----------------------------------------------------------------------------------------------------------------------
webview.clearCache(true); //清除缓存内容
----------------------------------------------------------------------------------------------------------------------
3.使用WebView控件装载HTML
WebView控件不仅可以通过URL装载网页,也可以直接装载HTML代码。WebView类有两个方法可以装载HTML,两个方法如下:
-------------------------------------------------------------------------------------------------------------
public void loadData(String data, String mimeType, String encoding)
public void loadDataWithBaseURL(String baseUrl, String data,
String mimeType, String encoding, String historyUrl)
------------------------------------------------------------------------------------------
其中,loadData方法的参数如下:
data: HTML代码
mimeType: Mine类型,一般为text/html
encoding: HTML代码的编码,例如GBK、utf-8
------------------------------------------------------------------------------------------
loadDataWidthBaseURL方法的参数含义如下:
--------------------------------------------------------------------------------------------
baseUrl:获取相对路径的根URL,如果设为null,默认值是about:blank
failUrl:如果HTML代码装载失败或为null时,WebView控件会装载这个参数指定的Url
其他参数:与loadData方法的参数含义相同
--------------------------------------------------------------------------------------------
代码示例如下:
xml文件代码如下:
--------------------------------------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button android:id="@+id/button_url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="NetURL"/>
<Button android:id="@+id/sdcard_url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/button_url"
android:text="SdcardURL"/>
<Button android:id="@+id/blank"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/sdcard_url"
android:text="Blank"/>
<Button android:id="@+id/load"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/blank"
android:text="load"/>
<WebView android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@id/button_url"/>
</RelativeLayout>
----------------------------------------------------------------------
java测试代码如下:
-----------------------------------------------------------------------
package com.example.webview;
import java.net.URLEncoder;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private WebView webview=null;
final String mimeType = "text/html";
final String encoding = "utf-8";
private Button mButtonInternetUrl=null;
private Button mButtonSdcardPng=null;
private Button mButtonBack=null;
private Button mButtοnlοad=null;
String url="http://www.google.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonInternetUrl = (Button)findViewById(R.id.button_url);
mButtonSdcardPng = (Button)findViewById(R.id.sdcard_url);
mButtonBack = (Button)findViewById(R.id.blank);
webview = (WebView)findViewById(R.id.webview);
mButtonload = (Button)findViewById(R.id.load);
webview.getSettings().setJavaScriptEnabled(true);
mButtonInternetUrl.setOnClickListener(this);
mButtonSdcardPng.setOnClickListener(this);
mButtonBack.setOnClickListener(this);
mButtonload.setOnClickListener(this);
}
/**
* appear the assigned webpage
*/
private void webHtml(){
try{
if(URLUtil.isNetworkUrl(url)){ //detect the url is the qualified
webview.loadUrl("http://www.google.com");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* appear the picture in the SDcard
*/
private void SdcardUrl(){
try {
webview.loadUrl("file:///sdcard/tencentOne.jpg");
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* go back the history page
*/
private void back(){
try {
webview.goBack();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* go forward the next webpage
*/
private void forward(){
try{
webview.goForward();
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* clear the access cache
*/
private void clearCache(){
try{
webview.clearCache(true);
}catch(Exception ex){
ex.printStackTrace();
}
}
private void localHtmlBlankSpace(){
try{
String data = " 测试包含空格的html数据 ";
/**
* 使用如下的语句将不会对空格做处理
*/
//webview.loadData(URLEncoder.encode(data, encoding), mimeType, encoding);
/**
* 使用下列语句将会对空格做相应的处理
*/
webview.loadData(URLEncoder.encode(data, encoding).replaceAll("\\+", " ")
, mimeType, encoding);
}catch(Exception ex){
ex.printStackTrace();
}
}
private void localHtmlLoad(){
try{
String data = "测试本地图片和文字混合显示," +"这是<IMG src=" +
"'\"file:///android_asset/icon.png\"/'>APK里的图片";
webview.loadDataWithBaseURL("about:blank", data,
mimeType, encoding, "");
}catch(Exception ex){
ex.printStackTrace();
}
}
protected void onDestroy(){
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.button_url:
webHtml();
break;
case R.id.sdcard_url:
SdcardUrl();
case R.id.blank:
localHtmlBlankSpace();
break;
case R.id.load:
localHtmlLoad();
break;
}
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,0,0,"前进");
menu.add(0,1,1,"后退");
menu.add(0,2,2,"清除");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case 0:
Toast.makeText(this, "item0", Toast.LENGTH_SHORT).show();
forward();
break;
case 1:
Toast.makeText(this, "item1", Toast.LENGTH_SHORT).show();
back();
break;
case 2:
Toast.makeText(this, "item2", Toast.LENGTH_SHORT).show();
clearCache();
break;
default:
break;
}
return true;
}
}
--------------------------------------------------------------------------------------------------------
测试显示结果如下:
-------------------------------------------------------------------------------------------------------