之前项目也有做过,今日有空就复习一下....但犯了一个很低级错误,竟然把JSInterface类的成员给static了,后面还wv.addJavascriptInterface(new JSInterface(),"JSInterface2"); 所以后面javascript调用java就出现抛出CloneNotSupportedException------“"Class doesn't implement Cloneable"”。。好了上代码
/*JsInterface.java*/
/*该类是JS调用JAVA端的,JAVA端调用webview中的JS很简单,后面代码将给出*/
public class JsInterface {
/*interface for javascript to invokes*/
public interface wvClientClickListener {
public void wvHasClickEnvent();
}
private wvClientClickListener wvEnventPro = null;
public void setWvClientClickListener(wvClientClickListener listener) {
wvEnventPro = listener;
}
public void javaFunction() {
if(wvEnventPro != null)
wvEnventPro.wvHasClickEnvent();
}
}
/*Js2JavaActivity.JAVA*/
/*程序入口*/
public class Js2JavaActivity extends Activity{
private Button btn_show,btn_hide;
private WebView wv;
private JsInterface JSInterface2 = new JsInterface();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.js2java);
btn_show = (Button)findViewById(R.id.btn_java2js_show);
btn_hide = (Button)findViewById(R.id.btn_java2js_hide);
wv = (WebView)findViewById(R.id.wv_js2java);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(JSInterface2,"JSInterface2");
wv.setWebViewClient(new webviewClient());
wv.loadUrl("file:///android_asset/index.html");
}
class webviewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click show button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(0)"));//这里是java端调用webview的JS
}
});
btn_hide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You click hide button", 1000).show();
wv.loadUrl(String.format("javascript:java2js(1)"));//这里是java端调用webview的JS
}
});
JSInterface2.setWvClientClickListener(new webviewClick());//这里就是js调用java端的具体实现
}
}
class webviewClick implements wvClientClickListener {
@Override
public void wvHasClickEnvent() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "link be on click", 1000).show();
}
}
}
/*好吧,照顾码农需要,将布局的也贴上*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_show"
android:text="click_show"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_java2js_hide"
android:text="click_hide"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<WebView
android:id="@+id/wv_js2java"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
/*最后是HTML文件*/
<html>
<body>
<script language="javascript">
function java2js(isHide) {
var x = document.getElementById('div');
if(isHide==0)
x.className = "";
else
x.className = "hide";
}
function js2java() {
JSInterface2.javaFunction();
}
</script>
<style type="text/css">
.hide {
display:none;
}
</style>
<a href="#" onClick="js2java()">js2java</a>
<div class="hide" id="div">
<font color="#FF0000">this is a hide div~~~~</font>
</div>
</body>