引用:http://www.oschina.net/code/snippet_97818_4400
[代码] java bean代码
01 |
public class Bean{ |
02 |
private String title; |
03 |
04 |
public void setTitle(String title){ |
05 |
this .title = title; |
06 |
} |
07 |
08 |
public String getTitle(){ |
09 |
return this .title; |
10 |
} |
11 |
} |
[代码] android端
01 |
package com.xu81.tw4a; |
02 |
03 |
import java.util.List; |
04 |
05 |
import android.app.Activity; |
06 |
import android.webkit.WebChromeClient; |
07 |
import android.webkit.WebSettings; |
08 |
import android.webkit.WebView; |
09 |
10 |
public class TiddlyWiki4Android extends Activity { |
11 |
12 |
private WebView mainWebView; |
13 |
14 |
/** Called when the activity is first created. */ |
15 |
@Override |
16 |
public void onCreate(Bundle savedInstanceState) { |
17 |
super .onCreate(savedInstanceState); |
18 |
setContentView(R.layout.main); |
19 |
|
20 |
//webview对象 |
21 |
mainWebView = (WebView) findViewById(R.id.mainWebView); |
22 |
23 |
WebSettings setting = mainWebView.getSettings(); |
24 |
setting.setJavaScriptEnabled( true ); //允许javascript |
25 |
mainWebView.setWebChromeClient( new WebChromeClient()); //初始化WebChromeClient对象 |
26 |
Bean b = new Bean(); |
27 |
b.setTitle( "title" ); |
28 |
mainWebView.addJavascriptInterface(b, "bean" ); //添加javascript对象 |
29 |
mainWebView.loadUrl( "file:///android_asset/test.html" );//加载本地html页面 |
30 |
} |
31 |
} |
[代码] javascript端调用方式
1 |
function getDefault(){ |
2 |
var bean = window.bean; |
3 |
var title = bean.getTitle(); |
4 |
alert(title); |
5 |
} |