Android通过WebView调用 JS 代码
a. webView.loadUrl("javascript:callJS()");
b. setWebChromeClient响应弹窗
1.布局
<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: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.index.html
<!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’才有响应"); } </script> </head> <body> <div id="test"> 0000000000 </div> </body> </html>
3.java
public class MainActivity extends Activity { WebView webView; Button buttonLeft, buttonRight; @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); 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.loadUrl("javascript:callJS()"); } }); } }); 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; } }); } }