http://yarin.iteye.com/?page=4

Activity01.java

package com.yarin.android.Examples_08_07;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView; public class Activity01 extends Activity {
private WebView mWebView;
private PersonalData mPersonalData; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPersonalData = new PersonalData();
mWebView = (WebView) this.findViewById(R.id.WebView01);
// 设置支持JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);
// 把本类的一个实例添加到js的全局对象window中,
// 这样就可以使用window.PersonalData来调用它的方法
mWebView.addJavascriptInterface(this, "PersonalData");
// 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框
// Sets the chrome handler. This is an implementation of WebChromeClient
// for use in handling JavaScript dialogs, favicons, titles, and the
// progress. This will replace the current handler.
mWebView.setWebChromeClient(new WebChromeClient() { @Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
} });
// 加载网页
// mWebView.loadUrl("file:///android_asset/PersonalData_new.html");
mWebView.loadUrl("file:///android_asset/PersonalData.html");
} // 在js脚本中调用得到PersonalData对象
@JavascriptInterface
public PersonalData getPersonalData() {
return mPersonalData;
} // js脚本中调用显示的资料
class PersonalData {
String mID;
String mName;
String mAge;
String mBlog; public PersonalData() {
this.mID = "123456789";
this.mName = "Android";
this.mAge = "100";
this.mBlog = "http://yarin.javaeye.com";
} public String getID() {
return mID;
} public String getName() {
return mName;
} public String getAge() {
return mAge;
} public String getBlog() {
return mBlog;
}
}
}

PersonalData_new.html

<html>
<head>
<script type="text/javascript">
function showAndroidToast(toast)
{
var personaldata_new = PersonalData.getPersonalData();
alert(personaldata_new);
var personaldata = window.PersonalData.getPersonalData();
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
} function myFunction(){
console.log("Write a new Line");//调试信息
alert("Hello World!");
} </script>
</head>
<body>
<div id = "Personaldata">
<p> Personal Data </p>
<button onclick="myFunction()">click here!</button>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
</div>
</body>
</html>

PersonalData.html

<html>
<head>
<script type="text/javascript">
function onload(){
var personaldata = window.PersonalData.getPersonalData();
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
}
</script>
</head>
<body onload="javascript:onload()">
<div id = "Personaldata">
<p> Personal Data </p>
</div>
</body>
</html>

或者

<html>
<head>
<script type="text/javascript" src="test2.js">
</script>
</head>
<body onload="javascript:myFunction()">
<div id = "Personaldata">
<p> Personal Data </p>
<button onclick="myFunction()">click here!</button>
</div>
</body>
</html>

test2.js

function myFunction(){
var personaldata = window.PersonalData.getPersonalData();
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
}

或者

<html>
<head>
<script type="text/javascript" src="test2.js">
</script>
</head>
<body><!-- onload="javascript:myFunction()"-->
<div id = "Personaldata">
<p> Personal Data </p>
<button onclick="myFunction()">click here!</button>
</div>
</body>
</html>

test2.js

window.onload= function(){
var personaldata = window.PersonalData.getPersonalData();
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
}

<script type="text/javascript" src="test2.js"/>android2.2兼容,但是android4.1.2不兼容。下面的才兼容android4.1.2

<script type="text/javascript" src="test2.js"></script>

4.2及以上版本必须在js调用的java方法加上@JavascriptInterface,4.1及以下版本不用加上注释。

Examples_08_07的更多相关文章

  1. 使用WebView出现web page not available

    很有可能是没有设置权限,所以不能浏览相应的URL,设置如下: 在AndroidManifest.xml中添加 <uses-permission android:name="androi ...

随机推荐

  1. 9 Palindrome Number(判断是否为回文数Easy)

    题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...

  2. bind函数

    bind函数把一个本地协议地址赋予一个套接字 对于网际协议,协议地址是32位的IPv4地址或128位的IPv6与16位的TCP或UDP端口号的组合 int bind ( int sockfd, con ...

  3. java Html2Image 实现html转图片功能

    //java Html2Image 实现html转图片功能 // html2image  HtmlImageGenerator imageGenerator = new HtmlImageGenera ...

  4. 转:7个鲜为人知却超实用的PHP函数

    PHP have lots of built-in functions, and most developers know many of them. But a few functions are ...

  5. 脚本两则--用于快速部署HADOOP,SPARK这些(特别是VM虚拟机模板部署出来的)。。

    感觉可能只是适合我自己的部署习惯吧,且只针对CENTOS6及以下版本,以后有时间,可能还是要改进.. 1,从VM的模块产生的虚拟机,如何快速搞定网络配置? #!/bin/bash #usage:./i ...

  6. HDU 1045 Fire Net(图匹配)

    题目大意: 这个是以前做过的一道DFS题目,当时是完全暴力写的. 给你一个N代表是N*N的矩阵,矩阵内 ‘X’代表墙, ‘.’代表通道. 问这个矩阵内最多可以放几个碉堡, 碉堡不能在同一行或者同一列, ...

  7. POJ1661 Help Jimmy

    Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9863 Accepted: 3201 Descriptio ...

  8. centos 7 卸载 mariadb 安装mysql

    1,卸载mariadbsystemctl stop mariadbrpm -qa | grep mariadbrpm -e --nodeps mariadb-5.5.52-1.el7.x86_64rp ...

  9. EasyUEFI

    ---------------------------------http://www.easyuefi.com/downloads/EasyUEFI_Setup.exe--------------- ...

  10. js的深拷贝和浅拷贝

    一.数组的深浅拷贝 在使用JavaScript对数组进行操作的时候,我们经常需要将数组进行备份,事实证明如果只是简单的将它赋予其他变量,那么我们只要更改其中的任何一个,然后其他的也会跟着改变,这就导致 ...