package com.bw.zhuangao2_13;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String Url="<!DOCTYPE html>\n" +
"<html>\n" +
"\t<head>\n" +
"\t\t<meta charset=\"gb2312\">\n" +
"\t\t<title></title>\n" +
"\t</head>\n" +
"\t<h1>郭老师,是蝈蝈,还是蛐蛐,他为啥没有触角</h1>\n" +
"\t<body>\n" +
"\t</body>\n" +
"</html>\n";
private WebView web;
private ProgressBar pra;
ProgressDialog progressDialog;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build. VERSION. SDK_INT>Build. VERSION_CODES.M){
requestPermissions(new String[]{
Manifest. permission.WRITE_EXTERNAL_STORAGE,
Manifest . permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE
},101);
}
web = (WebView) findViewById(R.id.web);
pra = (ProgressBar) findViewById(R.id.pra);
// web.loadUrl("https://www.baidu.com/");
// web.loadUrl(Url);
// web.loadData(Url,"text/html","utf-8");
// web.loadDataWithBaseURL(null,Url,"text/html","utf-8","null");
web.loadUrl("file:///android_asset/test.html");
// web.loadUrl("http://www.919.com.cn/index.php/Home/cate.html");
progressDialog=new ProgressDialog(this);
progressDialog.setMessage("进度对话框");
progressDialog.setTitle("对画框");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
progressDialog.dismiss();
}
});
// progressDialog.show();
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
progressDialog.show();
pra.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
pra.setVisibility(View.INVISIBLE);
}
});//设置对象必须写不写展示不出来
web.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
pra.setProgress(newProgress);
progressDialog.setProgress(newProgress);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
return true;
}
});
WebSettings webSettings=web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
web.addJavascriptInterface(this,"justTest");
}
@JavascriptInterface
public void test(){
Toast.makeText(MainActivity.this, "js 调用安卓", Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void callphone(){
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri uri=Uri.parse("tel:"+"18643861186");
intent.setData(uri);
startActivity(intent);
}
public void newawda(View view) {
// web.loadUrl("javascript:test()");
// web.evaluateJavascript("javascript:test2('天津')", new ValueCallback<String>() {
// @Override
// public void onReceiveValue(String s) {
// Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();
// }
// });
web.loadUrl("javascript:changetext('我是锅锅')");
web.loadUrl("javascript:changeimg('')");
}
}
专高2-day13
最新推荐文章于 2024-08-23 11:30:03 发布