1.布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="0dp"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="3">
<Button
android:id="@+id/btnLeft"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="左边" />
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="0"/>
<Button
android:id="@+id/btnRight"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="右边" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
2.java
package com.example.lyj.edit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.UriMatcher;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebHistoryItem;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import java.util.HashMap;
import java.util.Set;
public class MainActivity extends Activity {
WebView webView;
Button buttonLeft, buttonRight;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
buttonLeft = findViewById(R.id.btnLeft);
buttonRight = findViewById(R.id.btnRight);
editText = findViewById(R.id.edittext);
WebSettings webSettings = webView.getSettings();
//允许使用JS
webSettings.setJavaScriptEnabled(true);
// 设置允许JS弹窗
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("file:///android_asset/index.html");
buttonLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript("javascript:callJS()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
//将button显示的文字改成JS返回的字符串
buttonLeft.setText(s);
}
});
}
});
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//1. 根据协议的参数,判断是否是所需要的url
// 一般根据scheme(协议格式) & authority(协议名)判断(前两个参数)
//假定传入进来的 url = "js://webview?arg1=111&arg2=222"(同时也是约定好的需要拦截的)
Uri uri = Uri.parse(url);
// 如果url的协议 = 预先约定的 js 协议
// 就解析往下解析参数
if (uri.getScheme().equals("js"))
{
// 如果 authority = 预先约定协议里的 webview,即代表都符合约定的协议
// 所以拦截url,下面JS开始调用Android需要的方法
if(uri.getAuthority().equals("webview"))
{
// 执行JS所需要调用的逻辑,将edittext显示为第一个参数的值
editText.setText(uri.getQueryParameter("arg1"));
}
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("alert1");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
result.confirm();
}
});
b.setCancelable(false);
b.create().show();
return true;
}
});
}
}
3.html JS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Carson_Ho</title>
// JS代码
<script>
// Android需要调用的方法
function callJS(){
document.getElementById("test").innerHTML = "android button 点击左边 实现'webView.loadUrl('javascript:callJS()');'就可以响应";
alert("Android调用了JS的callJS方法 实现‘webView.setWebChromeClient’才有响应");
return "JS函数callJS的返回值";
}
function callAndroid(){
/*约定的url协议为:js://webview?arg1=111&arg2=222*/
document.location = "js://webview?arg1=参数一&arg2=参数2";
}
</script>
</head>
<body>
<div id="test" style="text-align:center;"> 0000000000 </div>
<body>
<button type="button" id="button1" onclick="callAndroid()">点击使用callAndroid调用Android代码</button>
</body>
</body>
</html>