Android和javascript之間互動,javascript需要運行在瀏覽器或者Android的WebView組件,javascript 必須還有載體html檔案,這個html檔案
放到Android項目assets檔案夾下面。
首先我把我把代碼先貼出來:
main.xml layout檔案的內容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send To JavaScript" />
<Button
android:id="@+id/btnNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send To JavaScript(No Parameter)" />
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
assets 檔案夾下面的html的內容如下:
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript">
function get4Android(str) {
document.getElementById("show").innerHTML = "This is a message from android:"
+ str;
}
function get4AndroidNoparameter() {
alert("get4AndroidNoparameter");
}
function send2Android() {
var str = document.getElementById("mess").value;
window.myjs.runOnAndroidJavaScript(str);//調用android的函數
}
function send2AndroidNoparameter() {
window.myjs.runOnAndroidJavaScript();//調用android的無參函數
}
</script>
</head>
<body>
<input type="text" id="mess" />
<input type="button" value="Send To Android(Having Parameters)" onclick="send2Android()" />
<input type="button" value="Send To Android(No parameters)" onclick="send2AndroidNoparameter()" />
<div id="show"></div>
</body>
</html>
Activity 的代碼如下:
package com.broadvision.jstest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class JsTestActivity extends Activity {
private EditText txt;
private WebView wv;
private Button btn;
private Button btnNo;
private Handler h = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (EditText) findViewById(R.id.txt);
wv = (WebView) findViewById(R.id.wv);
btn = (Button) findViewById(R.id.btn);
btnNo = (Button) findViewById(R.id.btnNo);
wv.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLightTouchEnabled(true);
webSettings.setSaveFormData(false);
webSettings.setSavePassword(false);
webSettings.setSupportZoom(false);
// Do not use addJavascriptInterface() unless all of the HTML in this
// WebView was written by you 一般情況下java和javascript不能相互調用,除非你知道javascript代碼是安全的.
// The Java object that is bound runs in another thread and not in the
// thread that it was constructed in. myjs是自己定義的,供javascript訪問的介面
wv.addJavascriptInterface(new runJavaScript(), "myjs");
// the WebChromeClient can invoke "alert()" js method
wv.setWebChromeClient(new WebChromeClient());
String url = "file:///android_asset/android.html";
wv.loadUrl(url);
//WebView要調用assets檔案夾下面的html檔案路徑格式如下:file:///android_asset/html檔案名稱
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 調用javascript的函數get4Android(str)
調用javascript有參的建構函式
wv.loadUrl("javascript:get4Android('" + txt.getText().toString() + "')");
wv.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
}
});
btnNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("No parameter");
// 調用javascript的函數get4AndroidNoparameter() Java 調用javascript無參的函數
wv.loadUrl("javascript:get4AndroidNoparameter()");
wv.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
}
});
}
// The Java object that is bound runs in another thread and not in the thread
// that it was constructed in.文檔的一句話!
class runJavaScript {// 這個Java 對象是綁定在另一個線程裡的,
public void runOnAndroidJavaScript(final String str) {
h.post(new Runnable() {
@Override
public void run() {// 這裡應該特別注意的
TextView show = (TextView) findViewById(R.id.show);
show.setText("This is a message from javascript:" + str);
wv.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
}
});
}
public void runOnAndroidJavaScript() {
h.post(new Runnable() {
@Override
public void run() {// 這裡應該特別注意的
Toast.makeText(JsTestActivity.this,
"This is a message from javascript", Toast.LENGTH_LONG).show();
wv.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
}
});
}
}
}
注意事項:
1.Java 可以根據方法名相同參數不同重載,javascript最好不要這樣。
2. WebView.setWebChromeClient(new WebChromeClient()); 設定以後 javascript的alert方法才可以再WebView中調用。
3.webSettings.setLightTouchEnabled(true); 和webView.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
溝通來解決EditText和WebView中的輸入框不能分別獲得焦點的問題。
項目
希望對大家有所協助,如果你需要這個項目,我也可以發給你。