// 启用javascript contentWebView.getSettings().setJavaScriptEnabled(true); // 从assets目录下面的加载html contentWebView.loadUrl("file:///android_asset/wst.html"); // 无参数调用 contentWebView.loadUrl("javascript:javacalljs()");
contentWebView.addJavascriptInterface(this, "wst");javainterface实际就是一个普通的java类,里面是我们本地实现的java代码, 将object 传递给webview,并指定别名,这样js脚本就可以通过我们给的这个别名来调用我们的方法,在上面的代码中,this是实例化的对象,wst是这个对象在js中的别名
mWebView.loadUrl("javascript:test(‘" + aa+ "‘)"); //aa是js的函数test()的参数
<div id=‘b‘><a onclick="window.wst.clickOnAndroid(2)">b.c</a></div>
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> <script type="text/javascript"> function javacalljs(){ document.getElementById("content").innerHTML += "<br\>java调用了js函数"; } function javacalljswithargs(arg){ document.getElementById("content").innerHTML += ("<br\>"+arg); } </script> </head> <body> this is my html <br/> <a onClick="window.wst.startFunction()">点击调用java代码</a><br/> <a onClick="window.wst.startFunction(‘hello world‘)" >点击调用java代码并传递参数</a> <br/> <div id="content">内容显示</div> </body> </html>
package wst.webview; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private WebView contentWebView = null; private TextView msgView = null; @SuppressLint("SetJavaScriptEnabled") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); contentWebView = (WebView) findViewById(R.id.webview); msgView = (TextView) findViewById(R.id.msg); // 启用javascript contentWebView.getSettings().setJavaScriptEnabled(true); // 从assets目录下面的加载html contentWebView.loadUrl("file:///android_asset/wst.html"); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(btnClickListener); contentWebView.addJavascriptInterface(this, "wst"); } OnClickListener btnClickListener = new Button.OnClickListener() { public void onClick(View v) { // 无参数调用 contentWebView.loadUrl("javascript:javacalljs()"); // 传递参数调用 contentWebView.loadUrl("javascript:javacalljswithargs(" + "‘hello world‘" + ")"); } }; public void startFunction() { Toast.makeText(this, "js调用了java函数", Toast.LENGTH_SHORT).show(); runOnUiThread(new Runnable() { @Override public void run() { msgView.setText(msgView.getText() + "\njs调用了java函数"); } }); } public void startFunction(final String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); runOnUiThread(new Runnable() { @Override public void run() { msgView.setText(msgView.getText() + "\njs调用了java函数传递参数:" + str); } }); } }
<?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" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="9" /> <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="text" /> </ScrollView> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="java调用js函数" /> </LinearLayout>
android webview js交互 第一节 (java和js交互)
原文:http://www.cnblogs.com/changyiqiang/p/6230973.html