HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function changeText(){
document.getElementById("p_id").innerText="aaaaaa";
}
function changeImg(){
document.getElementById("img_id").src="img/HBuilder.png";
}
function changeText(text){
document.getElementById("p_id").innerText=text
}
function changeImg(image){
document.getElementById("img_id").src=image;
}
function tusi(){
window.yu.tusi();
}
</script>
<title></title>
</head>
<body>
<p id="p_id">文字</p>
<img id="img_id" width="200" height="200" src="https://cn.bing.com/th?id=OSA.9ab1ef02419ca997bbd5ab4b80fadfd3&w=186&h=88&c=7&rs=2&qlt=80&cdv=1&pid=PopNow" />
<button id="button1_id" onclick="">谈对话框</button>
<button id="button2_id" onclick="changeText('bbbb')">改变文字</button>
<button id="button3_id" onclick="changeImg('img/HBuilder.png')">改变图片</button>
<button id="button4_id" onclick="tusi()">吐司</button>
</body>
</html>
MainActivity
package com.example.day13_javas;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.web);
webView.loadUrl("https://blog.csdn.net/qq_34178710/article/details/99689359");
webView.loadUrl("file:///android_asset/demo1.html");
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.addJavascriptInterface(this,"yu");
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setSavePassword(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
}
@JavascriptInterface
public void camera(){
Toast.makeText(this, "照相", Toast.LENGTH_SHORT).show();
}
public void click(View view) {
webView.loadUrl("JavaScript:image('https://cn.bing.com/th?id=OSA.fed6bb68fde245374a4c7b91ca77f546&w=186&h=88&c=7&rs=2&qlt=80&cdv=1&pid=PopNow')");
webView.clearHistory();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(webView!=null){
webView.destroy();
webView=null;
}
}
}