Android应用中怎么利用WebView与Javascript进行交互

发布时间:2020-11-26 16:32:22 作者:Leah
来源:亿速云 阅读:133

Android应用中怎么利用WebView与Javascript进行交互?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

(一)Android部分:

布局代码:

<LinearLayout 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">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
      android:id="@+id/input_et"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:singleLine="true"
      android:layout_weight="1"
      android:hint="请输入信息" />
    <Button
      android:text="Java调用JS"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="sendInfoToJs" />
  </LinearLayout>
  <WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

Activity代码:

/**
 * Android WebView 与 Javascript 交互。
 */
public class MainActivity extends ActionBarActivity {
  private WebView webView;
  @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
  @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);
    String url = "http://192.168.1.27/js_17_android_webview.html";
    webView.loadUrl(url);
    //在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 + "')");
  }
}

(二)网页代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>Android WebView 与 Javascript 交互</title>
<head>
 <style>
 body {background-color:#e6e6e6}
 .rect
 {
  color:white;
  font-family:Verdana, Arial, Helvetica, sans-serif;
  font-size:16px;
  width:100px;
  padding:6px;
  background-color:#98bf21;
  text-decoration:none;
  text-align:center;
  border:none;
  cursor:pointer;
 }
 .inputStyle {font-size:16px;padding:6px}
 </style>
</head>
<body>
 <p>测试Android WebView 与 Javascript 交互</p>
 <input id = "name_input" class = "inputStyle" type="text"/>
 <a class = "rect" onclick="sendInfoToJava()">JS调用Java</a>
 <script>
 function sendInfoToJava(){
  //调用android程序中的方法,并传递参数
  var name = document.getElementById("name_input").value;
  window.AndroidWebView.showInfoFromJs(name);
 }
 //在android代码中调用此方法
 function showInfoFromJava(msg){
  alert("来自客户端的信息:"+msg);
 }
 </script>
</body>
</html>

看完上述内容,你们掌握Android应用中怎么利用WebView与Javascript进行交互的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. App Webview与内嵌web交互实现
  2. 怎么在python中利用flask与JavaScript进行交互

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android webview javascript

上一篇:如何在Android中使用 AOP注解

下一篇:如何自Spring Boot中使用Logback对日志进行记录

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》