1. WebView 使用加载网页
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
1.1· 布局:
<?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"
tools:context="lanya.denganzhi.com.javaandjs.MainActivity"
android:orientation="vertical">
<!-- 使用WebView 加载网页 -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goWebView"
/>
<!-- Java 调用 Js -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JavatoJs"
android:onClick="JavatoJs"/>
<!-- WebView 控件显示网页内容 -->
<WebView
android:id="@+id/mywebview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></WebView>
</LinearLayout>
1.2. WebVew 加载网页
public void goWebView(View view) {
// webView = new WebView(this);
webView = (WebView)findViewById(R.id.mywebview);
WebSettings webSettings = webView.getSettings();
//设置支持javaScript脚步语言
webSettings.setJavaScriptEnabled(true);
//支持缩放按钮-前提是页面要支持才显示
webSettings.setBuiltInZoomControls(true);
//设置客户端-不跳转到默认浏览器中
webView.setWebViewClient(new WebViewClient());
//设置支持js调用java
// webView.addJavascriptInterface(new AndroidAndJSInterface(),"Android");
// 加载网页
// webView.loadUrl("https://www.baidu.com/");
//2. 设置支持js调用java
// 3. <input type="button" value="点击Android被调用" onclick="window.Android.showToast()" />
webView.addJavascriptInterface(new AndroidAndJSInterface(),"Android");
webView.loadUrl("file:///android_asset/JavaAndJavaScriptCall.html");
}
2. Java 如何调用Js ,必须首先使用WebView 加载网页内容
2.1. 加载本地网页,网页放在assets/JavaAndJavaScriptCall.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 javaCallJs(arg){
document.getElementById("content").innerHTML =
("欢迎:"+arg );
}
function showDialog(){
alert("你好,我是来自javascript");
}
</script>
</head>
<body>
<div align="left" id="content"> Java调用Js</div>
<div align="center">我是网页,我是网页</div>
<!--<p><img src="http://n.sinaimg.cn/default/1_img/upload/3933d981/655/w900h555/20200307/5bf7-iqmtvwv8331634.jpg"></p>-->
<input type="button" value="点击Android被调用" onclick="window.Android.showToast('from js')" />
</body>
</html>
Java 调用Js,拼接好格式即可
// Java 调用 Js, 用固定格式即可
// javaCallJs: 方法名
public void JavatoJs(View view) {
webView.loadUrl("javascript:javaCallJs(" + "'" + 110 + "'" + ")");
}
3. Js 调用 Java
// 1. 定义 Js 调用 Java 对应的 Java 接口
class AndroidAndJSInterface{
@JavascriptInterface
public void showToast(String title){
Toast.makeText(MainActivity.this, "我被js调用了:"+title, Toast.LENGTH_SHORT).show();
}
}
//2. 设置支持js调用java, 把类注入到WebView中
webView.addJavascriptInterface(new AndroidAndJSInterface(),"Android");
3. Js 实现代码: Js 调用 Java中的 showToast 方法
<input type="button" value="点击Android被调用" οnclick="window.Android.showToast('from js')" />
上图:
源码路径:https://download.csdn.net/download/dreams_deng/12234925