|
wex5开发的程序,调用本地的代码要用codova,为什么不把相关的代码开放出来,只要把代码写在里面就可以直接调了.下面是android上写的js调用java的部分代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wView = (WebView) findViewById(R.id.wView);
wView.loadUrl("file:///android_asset/demo1.html");
WebSettings webSettings = wView.getSettings();
//①设置WebView允许调用js
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("UTF-8");
//②将object对象暴露给Js,调用addjavascriptInterface
wView.addJavascriptInterface(new MyObject(MainActivity.this), "myObj");
}
<body>
<input type="button" value="JS调用Java(带参)" onclick="myObj.showToast('我是Js参数~');"/>
<input type="button" value="JS调用Java(不带参)" onclick="myObj.showDialog();"/>
</body> |
|