webView与JS交互

package com.bawei.html;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main2Activity extends AppCompatActivity {

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    webview = findViewById(R.id.webview);
   webview.setWebViewClient(new WebViewClient());
   webview.setWebChromeClient(new WebChromeClient());

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);

    webview.loadUrl("file:///android_asset/about.html");

   // webview.loadUrl("http://www.baidu.com");
}

public void callJS(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webview.evaluateJavascript("callJS()", null);
    }
}

}

你可能感兴趣的:(webView与JS交互)