android控件之webview和js与java交互
首先添加权限:<uses-permission android:name="android.permission.INTERNET"/>
布局文件:
<RelativeLayout 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" tools:context=".MainActivity" > <TextView android:id="@+id/preNums" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="当前进度:0%" /> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/preNums" /> </RelativeLayout>
html5页面代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"/> <meta name="format-detection" content="telephone=no"/> <title>手机网页</title> <style> *{ padding:0px; margin:0px;} div{ margin:0px auto;} .mytop{ height:60px; background-color:#000000; text-align:center;} .myfont{ color:#FFFFFF; font-size:16px; font-weight:bold; line-height:60px;} .mytest{ background-color:#FF0000; margin:20px auto; width:100px; height:100px; border-radius:50px; box-shadow:8px 8px 8px #0000FF; line-height:100px; text-align:center;} </style> <script> function ChangeDiv(){ //document.getElementById("insertDiv").innerHTML='哈哈'; //调用java代码 MyAndroid.MyFun("哈哈可以调用"); } // 被java调用的方法 function ToJavaFun(outstr){ document.getElementById("insertDiv").innerHTML=outstr; } </script> </head> <body> <div class="mytop"> <a href="http://www.baidu.com" class="myfont">前往百度</a> </div> <div class="mytest" id="insertDiv"> </div> <div style="text-align:center; padding:10px;"> <input name="" type="button" value="执行" onClick="ChangeDiv('你好');" > </div> </body> </html>
java代码:
package com.example.mywebview; import android.os.Bundle; import android.os.Handler; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.webkit.JavascriptInterface; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; import android.widget.Toast; /* * author:future * * sijienet.com * * */ @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" }) public class MainActivity extends Activity { WebView webView; TextView textView; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler = new Handler(); webView = (WebView) findViewById(R.id.webView1); textView = (TextView) findViewById(R.id.preNums); // 开启js接口模式 webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new MyAndroidFun(), "MyAndroid"); webView.setWebViewClient(new MyWebClient()); webView.setWebChromeClient(new MyChrome());// 配置默认参数 webView.loadUrl("file:///android_asset/index.html"); } class MyChrome extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { // 进度更新 textView.setText("当前进度:" + newProgress + "%"); super.onProgressChanged(view, newProgress); } } class MyAndroidFun {// 调用类作用于 @JavascriptInterface public void MyFun(String msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT) .show(); handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub String outString = "我的内容"; webView.loadUrl("javascript:ToJavaFun('" + outString + "')"); } }); } } @Override public void onBackPressed() { // TODO Auto-generated method stub if (webView.canGoBack()) { webView.goBack(); } // super.onBackPressed(); } class MyWebClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub if (url != null && url.length() > 0) { if (url.indexOf("http://") != -1 || url.indexOf("https://") != -1) {// 不存在 } else { // 添加前缀 url = "http://" + url; } view.loadUrl(url); return true; } return super.shouldOverrideUrlLoading(view, url); } } }