Html页面和Java代码结合的方式一般用在界面经常被更改 的情况下,可以讲html放在网络中,软件一打开就会访问网络获取到最新的界面。缺点是会受到网络信号的影响,从而导致访问速度慢。
1.用WebView来显示HTML代码
2.允许WebView执行JavaScript
webView.getSettings().setJavaScriptEnabled(true);
3.获取到HTML文件,也可从网络中获取
webView.loadUrl("file:///android_asset/index.html"); //HTML文件存放在assets文件夹中
4.添加一个对象, 让JS可以访问该对象的方法, 该对象中也可以调用JS中的方法
webView.addJavascriptInterface(new Contact(), "contact");
完整示例代码如下:
效果图:
MainActivity
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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)));
}
// Html调用此方法传递数据
public void showcontacts() {
String json = "[{\"name\":\"zxx\", \"amount\":\"9999999\", \"phone\":\"18600012345\"}]";
// 调用JS中的方法
webView.loadUrl("javascript:show('" + json + "')");
}
}
}
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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)));
}
// Html调用此方法传递数据
public void showcontacts() {
String json = "[{\"name\":\"zxx\", \"amount\":\"9999999\", \"phone\":\"18600012345\"}]";
// 调用JS中的方法
webView.loadUrl("javascript:show('" + json + "')");
}
}
}
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()" >
< 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 >
< 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()" >
< 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 >
拨打电话需要添加权限:
<uses-permission android:name="android.permission.CALL_PHONE" />