1.佈局javascript
<?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.javahtml
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 JSjava
<!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>