[img]
[/img]
[img]
[/img]
WebViewDemo.java
package com.test.activity; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Toast; import com.test.model.Person; public class WebViewDemo extends Activity { private WebView mWebView; private Handler mHandler = new Handler(); public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.webviewdemo); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.addJavascriptInterface(new Object() { List<Person> persons = new ArrayList<Person>(); /** * 由html页面中调用该方法,获得persons的size(),并动态的生成该数量的button */ public void clickOnAndroid() { mHandler.post(new Runnable() { public void run() { // System.out.println("hello"); int nums = 10; for (int i = 0; i < nums; i++) { Person person = new Person(); person.name = "name" + (i + 1); person.age = (i + 1) + ""; persons.add(person); } String javascript = "javascript:wave(" + nums + ")"; mWebView.loadUrl(javascript); } }); } /** * 由HTML页面调用,点击动态生成的button, * * @param num * list序号 */ public void show(int num) { System.out.println(num); Person p = persons.get(num); Toast .makeText(WebViewDemo.this, "姓名: " + p.name + " 年龄: " + p.age, Toast.LENGTH_SHORT).show(); }; }, "demo"); // 加载本地html页面 mWebView.loadUrl("file:///android_asset/demo.html"); } }
package com.test.model; public class Person { public String name; public String age; }