AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kingbright.webview_test">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Webview_test">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<WebView
android:id="@+id/wv_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="109dp"
tools:layout_editor_absoluteY="399dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.kingbright.webview_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
/**
* 2023-7-7
* webview使用URL 示例
*/
public class MainActivity extends AppCompatActivity {
private String myurl = "http://172.16.3.208/wwx/debug_kb_code/kb_code/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置软键盘不遮挡内容---webview
AndroidBug5497Workaround.assistActivity(this);
// 隐藏状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
WebView webView = findViewById(R.id.wv_webview);
// Android webView加载Html5页面,JS不能调用问题和click事件无效的解决方法
WebSettings s = webView.getSettings();
s.setBuiltInZoomControls(true);//进行控制缩放
s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//让webview只显示一列,也就是自适应页面大小,不能左右滑动;但在使用中发现,只针对4.4以下有效;
s.setUseWideViewPort(true);//让webview支持标签的viewport属性,前端html页面适配屏幕一般都会用到这个设置
s.setLoadWithOverviewMode(true);//缩放志屏幕大小
s.setSavePassword(true);//是否保存密码
s.setSaveFormData(true);//设置webview是否保存表单数据,默认true,保存数据
s.setJavaScriptEnabled(true);//允许支持js
s.setJavaScriptCanOpenWindowsAutomatically(true);//设置脚本是否允许自动打开弹窗,默认false,不允许;
s.setGeolocationEnabled(true);//设置定位是否可用,默认为false
s.setDomStorageEnabled(true);//启动webview的h5的本地存储功能,DOM存储api是否可用,默认false;
webView.requestFocus();//把输入焦点放在调用这个方法的控件上
webView.setScrollBarStyle(0);
// 重写alert()方法,避免js的alert()无效
webView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
result.confirm();
return true;
}
});
webView.loadUrl(myurl);
// 系统默认会通过手机浏览器打开网页;为了能够直接通过webview显示网页,则必须设置
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 该接口,主要是给webview提供时机,让其选择是否对URLloading进行拦截;
// 关于该接口的返回值,true--拦截webview加载url;false--允许webview加载url
view.loadUrl(url);
return true;
}
});
}
}
AndroidBug5497Workaround.java
package com.kingbright.webview_test;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
/**
* user
* 2022/8/5
* 设置软键盘不挡住输入框,引用时--AndroidBug5497Workaround.assistActivity(this);即可
*/
public class AndroidBug5497Workaround {
public static void assistActivity(Activity activity){
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent(){
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious){
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4) ){
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
}
else{
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight(){
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return r.bottom-r.top;// 全屏模式下: return r.bottom
}
}
结果