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;

public class WebViewDemo extends Activity {
private WebView mWebView;

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);

// 设置默认加载的可视范围是大视野范围
webSettings.setLoadWithOverviewMode(true);

// 加载本地asset文件
mWebView.loadUrl("file:///android_asset/demo.html");


// 加载非本地url
// mWebView.loadUrl("http://www.baidu.com.cn/");

}
}


注意修改Androidmanifest.xml

posted @ 2015-08-05 10:19  Flyoung  阅读(206)  评论(0编辑  收藏  举报