`
helpbs
  • 浏览: 1161639 次
文章分类
社区版块
存档分类
最新评论

Android WebView与网页JS相互调用

 
阅读更多

在Android中的写法:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final WebView web1=(WebView)findViewById(R.id.web1);

final Button mybutton=(Button)findViewById(R.id.myButton);

Handler h = new Handler();//声明一个传值的参数

web1.getSettings().setJavaScriptEnabled(true);//确定这个webview可以用JS

web1.addJavascriptInterface(new A(), "to2");//将本类对象添加到JS的全局对象window中.

web1.loadUrl("file:///android_asset/test.html");//这个是加载本工程的HTML地址,放在assets文件夹中

WebViewClient webclient=new WebViewClient(){

@Override

public void onPageFinished(WebView view, String url) {

// TODO Auto-generated method stub

super.onPageFinished(view, url);

}

};

web1.setWebViewClient(webclient);

}

class A{

public void a(){

这里做相应的逻辑操作。

}

在html当中的写法

<title>无标题文档</title>

<script type="text/javascript">

function f2(){

return window.to2.a();

}

</script>

</head>

<body>

<table align="center" width="724px" height="600px">

<tr align="center">

<td align="center">

<input name="submit" type="submit" value="跳转" onclick="return f2()" />

</td>

</tr>

</table>

</body>

</html>

注明:QQ技术交流群:108614806 感兴趣的加一下。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics