如开发是使用android stdio开发的话,需要参考
http://blog.csdn.net/lsyz0021/article/details/51473194
该文章建人建相应的目录和文件,直接上内容了,创建index.html,内容如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>学习</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"/>
<script type="text/javascript">
function jsconsole(){
document.getElementById("div_1").innerHTML="hello world";
}
</script>
</head>
<body>
<a>学习了................................</a>
<input type="button" value="点击Android被调用" οnclick="window.android.toast('abc')" />
<div id="div_1">
</div>
</body>
</html>
加截WebView 的activity Layout文件,文件名activity_web_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/tab_bg" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="callJs"
android:id="@+id/button22" />
</LinearLayout>
</LinearLayout>
activity 代码如下:
package com.example.thinkpad.myapplication;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.alibaba.fastjson.*;
import com.peidw.beans.Stud;
public class WebViewActivity extends BasicActivity {
private WebView webview;
private Button button22;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web_view);
button22=(Button)findViewById(R.id.button22);
webview=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new JSBridge(), "android");
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return true;
}
});
webview.loadUrl("file:///android_asset/index.html");
button22.setOnClickListener(new Button.OnClickListener(){//创建监听
public void onClick(View v) {
webview.loadUrl("javascript:jsconsole()" );
}
});
}
public class JSBridge{
@JavascriptInterface
public String toast(String str) {
Toast.makeText(getApplicationContext(), "传入的参数是" + str, Toast.LENGTH_SHORT).show();
return "我是android信息";
}
@JavascriptInterface
public void call(String phone) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
startActivity(intent);
}
}
}
还可以能考:
http://www.jianshu.com/p/a25907862523