android webview 加载本地html 实现 与 java 之间的相互响应
来源:互联网 发布:在线影视自动采集源码 编辑:程序博客网 时间:2024/06/10 13:06
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <WebView android:layout_below="@+id/tv" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/></RelativeLayout>
java
package com.example.webview_workflowy;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.webkit.WebView;import android.widget.Toast;public class MainActivity extends Activity {private WebView webView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //加载页面 webView = (WebView) findViewById(R.id.webview); //允许JavaScript执行 webView.getSettings().setJavaScriptEnabled(true); //找到Html文件,也可以用网络上的文件 webView.loadUrl("file:///android_asset/index.html"); // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法 webView.addJavascriptInterface(new Contact(), "contact"); } private final class Contact { //JavaScript调用此方法拨打电话 public void call(String phone) {// startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone))); Toast.makeText(MainActivity.this, phone, Toast.LENGTH_LONG).show(); } //Html调用此方法传递数据 public void showcontacts() { String json = "[{\"name\":\"zxx\", \"amount\":\"9999999\", \"phone\":\"18600012345\"}]"; // 调用JS中的方法 webView.loadUrl("javascript:show('" + json + "')"); } public void toast(String str){ Toast.makeText(MainActivity.this, "aaaaaaaaaaaa --- " + str, Toast.LENGTH_LONG).show(); } }}
html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function show(jsondata){ var jsonobjs = eval(jsondata); var table = document.getElementById("personTable"); for(var y=0; y<jsonobjs.length; y++){ var tr = table.insertRow(table.rows.length); var td1 = tr.insertCell(0); var td2 = tr.insertCell(1); td2.align = "center"; var td3 = tr.insertCell(2); td3.align = "center"; td1.innerHTML = jsonobjs[y].name; td2.innerHTML = jsonobjs[y].amount; td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>"; } } </script> </head> <body onload="javascript:contact.showcontacts()"> <button id="button" onclick = "javascript:contact.toast('123')">haha</button> <table border="0" width="100%" id="personTable" cellspacing="0"> <tr> <td width="30%">姓名</td> <td width="30%" align="center">存款</td> <td align="center">电话</td> </tr> </table> </body></html>
代码:http://download.csdn.net/my
- android webview 加载本地html 实现 与 java 之间的相互响应
- android webview 加载本地html 实现 与 java 之间的相互响应
- 使用WebView加载本地html页面,实现与java之间的相互响应
- Android使用webview控件加载本地html,通过Js与后台Java实现数据的传递
- Android的webview加载本地html、assert内html和网络URL&&& JS与移动端webview的相互交互
- Android中WebView加载本地Html,与JavaScript与Android方法相互传值
- Android中WebView加载本地Html,与JavaScript与Android方法相互传值(续)
- android中webview加载本地html并且相互交互
- Android中WebView载入本地HTML代码并实现Java与JavaScript交互的例子
- Android加载网页JavaScript与Java之间的相互调用
- Android webview加载本地html实现跨域访问
- Android WebView js 与 java 之间相互通信
- Android WebView无法加载本地html文件的解决方法
- Android WebView 加载本地SD卡的html
- Android在webview下加载本地html的方式
- Android之WebView与原生js之间的相互调用
- Android WebView加载本地Html文件
- android stuido WebView 如何本地加载html
- 将txt文件转换成excel文件的方法
- Android 简介
- Linux Core Dump
- #、##和__VA_ARGS__
- ubuntu 抓图
- android webview 加载本地html 实现 与 java 之间的相互响应
- Visual Studio 2010 快捷键
- 环球金融中心面试回来--不要怕~
- HDU1166:敌兵布阵(线段树单点更新)
- IOS文件常用操作
- linux 网络编程:客户端与服务器通过TCP协议相互通信
- Android BuildConfig.DEBUG的妙用
- Jquery.validate 驗證方法擴展小結
- Object类、多态