android和js互调

来源:互联网 发布:mac锐捷客户端已损坏 编辑:程序博客网 时间:2024/05/17 02:40

第一步:

assets下新建web.html文件:

  

<html><head><meta http-equiv = "Content-Type" content="text/html;charset=UTF-8"><script type="text/javascript">function javacalljs(){document.getElementById("content").innerHTML=("<br\>JAVA调用了JS的无参函数"); }  function javacalljswith(arg){document.getElementById("content").innerHTML=("<br\>"+arg); } </script></head><body>HTML内容显示<br/><h1><div id="content">内容显示</div></h1><br/><input type="button" value="点击调用java代码" onclick="window.android.startFunction()"/><br/><input type="button" value="点击调用java代码并传递参数" onclick="window.android.startFunction('http://blog.csdn.net/Leejizhou')"/></body></html>


第二步 android 代码

 布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.chinafeisite.tianbu.a905test.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android"        />    <Button        android:id="@+id/button"        android:layout_margin="8dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="调用JS"        android:background="@color/colorAccent"        android:textColor="#ffffff"        />    <Button        android:id="@+id/button2"        android:layout_margin="8dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="调用有参JS"        android:background="@color/colorAccent"        android:textColor="#ffffff"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="WebView"        android:layout_marginTop="8dp"        />    <WebView        android:id="@+id/webview"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>

第三步activity代码

  contentWebView = (WebView) findViewById(R.id.webview);        // 启用javascript        contentWebView.getSettings().setJavaScriptEnabled(true);        // 从assets目录下面的加载html        contentWebView.loadUrl("file:///android_asset/web.html");        contentWebView.addJavascriptInterface(MainActivity.this,"android");        //Button按钮 无参调用HTML js方法        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 无参数调用 JS的方法                contentWebView.loadUrl("javascript:javacalljs()");            }        });        //Button按钮 有参调用HTML js方法        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 传递参数调用JS的方法                contentWebView.loadUrl("javascript:javacalljswith(" + "'http://blog.csdn.net/Leejizhou'" + ")");            }        });        }    //由于安全原因 targetSdkVersion>=17需要加 @JavascriptInterface    //JS调用Android JAVA方法名和HTML中的按钮 onclick后的别名后面的名字对应    @JavascriptInterface    public void startFunction(){        runOnUiThread(new Runnable() {            @Override            public void run() {                Toast.makeText(MainActivity.this,"show",3000).show();            }        });    }    @JavascriptInterface    public void startFunction(final String text){        runOnUiThread(new Runnable() {            @Override            public void run() {                new AlertDialog.Builder(MainActivity.this).setMessage(text).show();            }        });

不要忘了网络权限

原创粉丝点击