85 lines
2.8 KiB
Java
85 lines
2.8 KiB
Java
|
|
package com.qxcm.moduleutil.activity;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.net.Uri;
|
||
|
|
import android.os.Build;
|
||
|
|
import android.os.Bundle;
|
||
|
|
import android.text.TextUtils;
|
||
|
|
import android.util.Log;
|
||
|
|
import android.view.View;
|
||
|
|
import android.webkit.JavascriptInterface;
|
||
|
|
import android.webkit.ValueCallback;
|
||
|
|
import android.webkit.WebSettings;
|
||
|
|
import android.webkit.WebView;
|
||
|
|
import android.webkit.WebViewClient;
|
||
|
|
import android.widget.Toast;
|
||
|
|
|
||
|
|
import androidx.activity.EdgeToEdge;
|
||
|
|
import androidx.appcompat.app.AppCompatActivity;
|
||
|
|
import androidx.core.graphics.Insets;
|
||
|
|
import androidx.core.view.ViewCompat;
|
||
|
|
import androidx.core.view.WindowInsetsCompat;
|
||
|
|
|
||
|
|
import com.alibaba.android.arouter.facade.annotation.Autowired;
|
||
|
|
import com.qxcm.moduleutil.R;
|
||
|
|
import com.qxcm.moduleutil.databinding.ActivityWebViewBinding;
|
||
|
|
|
||
|
|
import java.util.Map;
|
||
|
|
/**
|
||
|
|
*@author qx
|
||
|
|
*@data 2025/6/3
|
||
|
|
*@description: webview公共方法
|
||
|
|
*/
|
||
|
|
public class WebViewActivity extends BaseAppCompatActivity<ActivityWebViewBinding> {
|
||
|
|
|
||
|
|
public String title;
|
||
|
|
public String url;
|
||
|
|
@Override
|
||
|
|
protected void initData() {
|
||
|
|
WebSettings webSettings = mBinding.webView.getSettings();
|
||
|
|
webSettings.setUseWideViewPort(true);
|
||
|
|
webSettings.setLoadWithOverviewMode(true);
|
||
|
|
webSettings.setJavaScriptEnabled(true);
|
||
|
|
//增加JSBridge
|
||
|
|
mBinding.webView.addJavascriptInterface(new WebAppInterface(this), "Android");
|
||
|
|
// mBinding.webView.addJavascriptInterface(new WebViewBridgeConfig(title), WebViewBridgeConfig.NAME);
|
||
|
|
webSettings.setBuiltInZoomControls(false);
|
||
|
|
webSettings.setSupportZoom(false);
|
||
|
|
webSettings.setDomStorageEnabled(true);
|
||
|
|
webSettings.setBlockNetworkImage(false);//解决图片不显示
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
|
|
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
||
|
|
}
|
||
|
|
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
|
||
|
|
mBinding.webView.setHorizontalScrollBarEnabled(false);//水平不显示
|
||
|
|
mBinding.webView.setVerticalScrollBarEnabled(false); //垂直不显示
|
||
|
|
mBinding.webView.requestFocus();
|
||
|
|
mBinding.webView.loadUrl(url);
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
@Override
|
||
|
|
protected void initView() {
|
||
|
|
title = getIntent().getStringExtra("title");
|
||
|
|
url = getIntent().getStringExtra("url");
|
||
|
|
mBinding.topBar.setTitle(title);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected int getLayoutId() {
|
||
|
|
return R.layout.activity_web_view;
|
||
|
|
}
|
||
|
|
public class WebAppInterface {
|
||
|
|
Context mContext;
|
||
|
|
|
||
|
|
WebAppInterface(Context c) {
|
||
|
|
mContext = c;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 被 H5 调用的方法
|
||
|
|
@JavascriptInterface
|
||
|
|
public void showToast(String toast) {
|
||
|
|
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|