Android WebView js与Java调用实例
2014-04-09 11:20
435 查看
本文讲解了JavaScript与Java之间的相互的调用的一个小例子,该知识点在实际开发中经常用到。
布局文件main:
MainActivity:
assets目录下的myfile.html源码:
源码下载:
http://download.csdn.net/detail/u012440207/7165455
布局文件main:
<?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/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:text="text" /> </ScrollView> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="java调用js函数-无参" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="java调用js函数-有参" /> </LinearLayout>
MainActivity:
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; @SuppressWarnings("unused") public class MainActivity extends Activity { private WebView webView = null; private TextView textView = null; private Button button1, button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView) findViewById(R.id.webview); textView = (TextView) findViewById(R.id.textView); // 启用javascript webView.getSettings().setJavaScriptEnabled(true); // 从assets目录下面的加载html webView.loadUrl("file:///android_asset/wst.html"); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(btnClickListener1); button2.setOnClickListener(btnClickListener2); webView.addJavascriptInterface(this, "wst"); } /** * Java调用js函数--无参数调用 */ OnClickListener btnClickListener1 = new Button.OnClickListener() { public void onClick(View v) { // 无参数调用 webView.loadUrl("javascript:javacalljs()"); } }; /** * Java调用js函数--有参调用 */ OnClickListener btnClickListener2 = new Button.OnClickListener() { public void onClick(View v) { // 传递参数调用 webView.loadUrl("javascript:javacalljswithargs(" + "'hello world****'" + ")"); } }; /** * js调用Java函数--无参 */ public void startFunction() { runOnUiThread(new Runnable() { @Override public void run() { textView.setText(textView.getText() + "\njs调用了java函数"); } }); } /** * js调用了Java函数--有参 * @param str */ public void startFunction(final String str) { runOnUiThread(new Runnable() { @Override public void run() { textView.setText(textView.getText() + "\njs调用了java函数传递参数:" + str); } }); } }
assets目录下的myfile.html源码:
<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\>java调用了js函数--参数为:"+arg); } </script> </head> <body> 牛奶咖啡 <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>
源码下载:
http://download.csdn.net/detail/u012440207/7165455
相关文章推荐
- android webview java调用js方法没反应
- Android WebView详解之JS调用Java方法
- Android WebView的使用(三)js、java代码调用出错
- Android中webview里js和java的相互调用--含代码demo解析
- Android的WebView通过JS调用java代码
- android中webview调用js方法,js调用java方法
- Android基于WebView的Java和JS相互调用实现步骤,附带精简可行的DEMO
- Android WebView java和js互相调用
- Android WebView的js中调用java代码
- android Webview 实现js调用java代码实现Activity跳转
- Android WebView详解之JS调用Java方法
- android webview中js与java互相调用
- android中webView中js调用java无效的问题
- android中webviewjs与java的相互调用
- android webView中js调用java函数的关键之处
- Android的WebView中js与java代码相互调用
- Android WebView JS不能调用Java对象的问题
- Android WebView JS不能调用Java对象的问题
- android webView开发之js调用java代码示例
- android中webviewjs与java的相互调用