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="跳转" οnclick="return f2()" />

</td>

</tr>

</table>

</body>

</html>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值