Android使用WebView的简单使用

layout下 : 

 

webviewdemo.xml  文件

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical" >

 

    <WebView

        android:id="@+id/webview"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

 

</LinearLayout>

 

 

assets下 : 

 

demo.html  文件

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

<html>  

<head>  

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  

    <title>这是一个HTML</title>  

  

    

</head>  

    <body>  

       <br>

    <br>大家晚上好

    <br>

    <br>大家晚上好

    <br>

    <br>大家晚上好

    <br>

 

 

  <input type="button" value="测试" onclick="javascript:window.handler.show(document.body.innerHTML);"  />

  </body> 

    </body>  

</html>

 

 

Java文件 

 

WebViewDemo.java文件

 

package com.yw.webkitdemo;

 

import com.example.hhh.R;

 

import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

import android.os.Handler;

import android.webkit.WebSettings;

import android.webkit.WebSettings.LayoutAlgorithm;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Toast;

 

@SuppressLint("JavascriptInterface")

public class WebViewDemo extends Activity {

private WebView mWebView;

private Handler mHandler = new Handler();

 

// 定义handler接口

class Handler {

public void show(String data) {

new AlertDialog.Builder(WebViewDemo.this).setMessage(data).create()

.show();

}

}

 

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.webviewdemo);

mWebView = (WebView) findViewById(R.id.webview);

WebSettings webSettings = mWebView.getSettings();

 

/**

 * 如果是像上面加载百度的url的话,需要设置是否支持JavaScript,

 * 如果不支持JavaScript的话就会启动手机自带的浏览器去加载你要加载的URL, 否则会自己加载。设置是否支持JavaScript的

 */

webSettings.setJavaScriptEnabled(true);

// 还支持以下属性

// setAllowFileAccess 启用或禁止WebView访问文件数据

// setBlockNetworkImage 是否显示网络图像

// setBuiltInZoomControls 设置是否支持缩放

// setDefaultFontSize 设置默认的字体大小

// setDefaultTextEncodingName 设置在解码时使用的默认编码

// setFixedFontFamily 设置固定使用的字体

// setJavaScriptEnabled 设置是否支持JavaScript

// setLayoutAlgorithm 设置布局方式

// setLightTouchEnabled 设置用鼠标激活被选项

// setSupportZoom 设置是否支持变焦

// setUseWideViewPort 方法设置webview推荐使用的窗口。是否可任意比例缩放

// setLoadWithOverviewMode 方法是设置webview加载的页面的模式。

// setSavePassword

// setSaveFormData 保存表单数据

// setJavaScriptEnabled

// setRenderPriority

// setGeolocationEnabled 启用地理定位

// setGeolocationDatabasePath 设置定位的数据库路径

// setCacheMode 设置缓冲的模式

// setDomStorageEnabled 开启 DOM storage API 功能

// setDatabaseEnabled 开启 database storage API 功能

// setDatabasePath 设置数据库缓存路径

// webSettings.setAppCachePath 设置 Application Caches 缓存目录

// webSettings.setAppCacheEnabled 开启 Application Caches 功能

// 设置可以支持缩放

webSettings.setSupportZoom(true);

// 设置出现缩放工具

webSettings.setBuiltInZoomControls(true);

// 设置可在大视野范围内上下左右拖动,并且可以任意比例缩放

webSettings.setUseWideViewPort(true);

// 设置默认加载的可视范围是大视野范围

webSettings.setLoadWithOverviewMode(true);

// “通知”html有这么一个接口存在,并调用

mWebView.addJavascriptInterface(new Handler(), "handler");

// 自适应屏幕

webSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

// 如果你的网页不想让缩放,并且希望页面能够自适应手机屏幕的,可以在你的html文件中添加下面的属性:

/*

 * <meta name="viewport" content=

 * "width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"

 * > <meta name="mobileOptimized" content="width"> <meta

 * name="handheldFriendly" content="true">

 */

 

// 默认情况下,点击WebView所加载的页面上的超链接按钮时,是启动系统自带的浏览器去加载新的页面。

//如果要让WebView自己加载,需要添加如下代码

//如果使用SelfWebViewClient,需要自行定义SelfWebViewClient

mWebView.setWebViewClient(new WebViewClient());

 

// 加载html字符串

// webView.loadDataWithBaseURL(null, "<html>正在连接网络。。。</html>",

// "text/html", "UTF-8", null);

// 加载本地asset文件

mWebView.loadUrl("file:///android_asset/demo.html");

 

// 在的html中按钮的点击事件使用了一个接口:window.handler。

mWebView.setWebViewClient(new WebViewClient() {

@Override

public void onPageFinished(WebView view, String url) {

Toast.makeText(WebViewDemo.this, "网页加载完成", 0).show();

view.loadUrl("javascript:window.handler.show(document.body.innerHTML);");

super.onPageFinished(view, url);

}

});

 

// 加载url

// mWebView.loadUrl("http://www.baidu.com.cn/");

 

}

}

 

posted @ 2015-08-27 08:58  Flyoung  阅读(407)  评论(0编辑  收藏  举报