下面是代碼: html
@Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); this.loadUrl("file:///android_asset/www/index.html"); //給js設置調用的方法 this.appView.addJavascriptInterface(new JavaScripdtObject(), "android"); } /** * 供js調用的對象方法 * * @author YUJIAN * */ public class JavaScripdtObject { @JavascriptInterface//加入這個註解 public void runAndroidMethod(String str) { Toast.makeText(MainActivity.this, "被js調用"+str, Toast.LENGTH_SHORT) .show(); } }//js代碼
function getNode4() { var str = "javascrip木德"; //調用android代碼 window.android.runAndroidMethod(str); } </script> <input type="button" value="獲取頁面中的節點" onclick="getNode4()" />