Examples_08_07
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的更多相关文章
- 使用WebView出现web page not available
很有可能是没有设置权限,所以不能浏览相应的URL,设置如下: 在AndroidManifest.xml中添加 <uses-permission android:name="androi ...
随机推荐
- 初涉JavaScript模式 (7) : 原型模式 【三】
组合使用构造函数模式和原型模式 上篇,我们提到了原型模式的缺点,就是每个实例不能拥有自己的属性,因为纯原型模式所有的属性都是公开给每个实例的,故我们可以组合使用构造函数模式和原型模式.构造函数用来定义 ...
- python使用platform模块获取系统环境并去除换行符
近来在porting一个网站,企图拿到这个网站的数据来做分析.为了支持多系统环境的正常运行.需要知道当前系统环境的是什么OS? 1.python内置platform库.可以很方便得到当前系统环境时什么 ...
- C#程序中将图片转换为二进制字符串,并将二进制字符串转换为图片
/// <summary> /// 将图片以二进制流 /// </summary> /// <param name="path"></pa ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- net Random 随机数重复的问题
在实际项目中不仅需要随机产生密码字符串,还要一次生成多个.我把生成随机字符串的方法放到for循环中,问题出现了. 生成的字符串,会重复. 经过多方查证,原因在代码. //使用与系统时间相关的种子 Ra ...
- 实验四:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- java 代码格式(转)
//转至博客:http://developer.51cto.com/art/201202/320317.ht /** * Java编码格式个人推荐,参考JDK源码和Hyperic HQ源码(原spri ...
- .net反射详解
前言 之所以要写这篇关于C#反射的随笔,起因有两个: 第一个是自己开发的网站需要用到 其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...
- sql server 2005中使用with实现递归
WITH fw_requestion_note_temp(old_apply_id) AS ( --取根节点放入临时表 SELECT old_apply_id FROM fw_requestion_n ...
- Java---多线程的加强(1)
简单应用: 首先来看一个简单的例子: 两个线程,分别实现对1-100内的奇数,偶数的输出. 第一种方法:通过接口 MyRun类: package thread.hello; /** * 通过实现Run ...