Android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用。

一 Android部分:

     布局代码:

xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:focusable="true"    android:focusableInTouchMode="true"    android:orientation="vertical"    android:padding="8dp"    tools:context=".MainActivity">            android:layout_width="match_parent"        android:layout_height="wrap_content">                    android:id="@+id/input_et"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:singleLine="true"            android:layout_weight="1"            android:hint="请输入信息" />                    android:text="Java调用JS"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="sendInfoToJs" />                android:id="@+id/webView"        android:layout_width="match_parent"        android:layout_height="match_parent" />
Activity代码:

public class MainActivity extends AppCompatActivity {    private WebView webView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        webView = (WebView) findViewById(R.id.webView);        webView.setVerticalScrollbarOverlay(true);        //设置WebView支持JavaScript        webView.getSettings().setJavaScriptEnabled(true);        webView.loadUrl("file:///android_asset/webview.html");        //js中调用本地java方法        webView.addJavascriptInterface(new JsInterface(this), "AndroidWebView");//        //添加客户端支持        webView.setWebChromeClient(new WebChromeClient());    }    private class JsInterface {        private Context mContext;        public JsInterface(Context context) {            this.mContext = context;        }        //js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。        @JavascriptInterface        public void showInfoFromJs(String name) {            Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show();        }    }    //java中调用js代码    public void sendInfoToJs(View view) {        String msg = ((EditText) findViewById(R.id.input_et)).getText().toString();        //调用js中的函数:showInfoFromJava(msg)        webView.loadUrl("javascript:showInfoFromJava('" + msg + "')");//        webView.loadUrl("javascript:showInfoFromJava()");    }}
二  网页代码

html>lang="en">   charset="UTF-8">   </span>Android WebView <span style="font-family:'宋体';">与 </span>Javascript <span style="font-family:'宋体';">交互</span><span style="color:#e8bf6a;">type="button" value="分享" οnclick="f1()">type="text" id="show"/>        
注意:  android 调用js代码可能会报错如下:

 W/WebView(2088): java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.

解决办法:

webView.post(new Runnable() {    @Override    public void run() {        
      webView.loadUrl("javascript:showInfoFromJava('" + msg + "')");
}}) ;

更多相关文章

  1. Android截取开机关机事件
  2. Android(安卓)Compatibility zoom dialog 关闭
  3. Dealing with bitmap object in android NDK
  4. android > 调用拨打电话 并子线程监控然后返回跳转
  5. android调用系统发短信界面功能
  6. android 触发器
  7. Android(安卓)获取版本号
  8. Android(安卓)源代码结构
  9. Android(安卓)Wifi Hotspot Manager Class

随机推荐

  1. Android平板开发注意点
  2. Android四款系统架构工具
  3. Android书籍分享
  4. Android中图片占用内存的计算
  5. Android中的几种网络请求方式详解
  6. android中的sqlite数据库加密
  7. Android(安卓)原生开发、H5、React-Nativ
  8. Android(安卓)ImageView 总结【转载】
  9. Android下获取Root权限和设置目录属性
  10. Android(安卓)操作SQLite数据库之一