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"android:orientation="vertical"tools:context=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center|center_horizontal|center_vertical"android:background="@drawable/bj119201080"tools:ignore="ExtraText,MissingConstraints"><LinearLayoutandroid:id="@+id/main"android:layout_width="160sp"android:layout_height="wrap_content"android:layout_gravity="center|top"android:layout_marginTop="120sp"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="32sp"android:layout_gravity="center|top"android:background="@drawable/shebei"android:orientation="vertical"></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="32sp"android:layout_gravity="center|top"android:layout_marginTop="6sp"android:background="@drawable/ip"android:orientation="vertical"><EditTextandroid:id="@+id/tab1editTextIP"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerVertical="true"android:layout_marginLeft="56sp"android:background="@android:color/transparent"android:ems="19"android:text="192.168.1.199"android:textColor="@color/white"android:textSize="14sp"></EditText></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center|top"android:layout_marginTop="22sp"android:orientation="vertical"><Buttonandroid:id="@+id/tab1buttonConnect"android:layout_width="70sp"android:layout_height="30sp"android:layout_weight="1"android:background="@drawable/selector_green_bg"android:focusableInTouchMode="false"android:textColor="@color/gray"android:textSize="18sp" /></LinearLayout></LinearLayout><com.wang.avi.AVLoadingIndicatorViewandroid:id="@+id/load"android:layout_width="wrap_content"android:layout_height="wrap_content"app:indicatorName="LineSpinFadeLoaderIndicator"/></RelativeLayout ></androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.visualizedh5app;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;import com.wang.avi.AVLoadingIndicatorView;public class MainActivity extends AppCompatActivity {private WebView webView;private boolean isSuccess = false;private boolean isError = false;private Button tab1buttonConnect;private LinearLayout main;public EditText ip;private SharedPreferences mSps;private AVLoadingIndicatorView avi;private long time =0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);avi = (AVLoadingIndicatorView) findViewById(R.id.load);avi.hide();tab1buttonConnect = (Button) findViewById(R.id.tab1buttonConnect);main = (LinearLayout) findViewById(R.id.main);ip = (EditText) findViewById(R.id.tab1editTextIP);mSps = MainActivity.this.getSharedPreferences("share", MODE_PRIVATE);String strIp = mSps.getString("ip", "192.168.1.12");ip.setText(strIp);ip.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {SharedPreferences.Editor editor = mSps.edit();editor.putString("ip", ip.getText().toString());editor.commit();}});//连接按钮点击事件tab1buttonConnect.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {main.setVisibility(View.GONE);//隐藏//main.setVisibility(View.VISIBLE);显示avi.show();String url = "http://" + ip.getText().toString();System.out.println("--------------" + url);setUpView(url);//加载h5页面结束}});}@SuppressLint("SetJavaScriptEnabled")public void setUpView(String url) {//加载需要显示的网页webView = new WebView(this);WebSettings webSettings = webView.getSettings();webView.loadUrl(url);webSettings.setJavaScriptEnabled(true); //设置WebView属性,运行执行js脚本webSettings.setDomStorageEnabled(true);webSettings.setUseWideViewPort(true);//设定支持viewportwebSettings.setLoadWithOverviewMode(true); //自适应屏幕webSettings.setBuiltInZoomControls(true);webSettings.setDisplayZoomControls(false);webSettings.setSupportZoom(true);//设定支持缩放webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不读取缓存 webView.setBackgroundColor(ContextCompat.getColor(this,android.R.color.transparent));webView.setBackgroundResource(R.color.black);webView.setWebViewClient(webClient); //设置Web视图}// webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);不读取缓存WebViewClient webClient = new WebViewClient() {//设置打开网页时,不调用系统浏览器进行打开,而是在本WebView中直接显示。@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);
// Toast.makeText(MainActivity.this, "开始加载", Toast.LENGTH_SHORT).show();return true;}@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);}@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);if (!isError) {isSuccess = true;System.out.println("网页打开成功");setContentView(webView);//回调成功后的相关操作} else {isSuccess = false;System.out.println("网页打开失败");startActivity(new Intent(MainActivity.this, MainActivity.class));// 留在当前页面Toast.makeText(MainActivity.this, "提示:连接失败,请检查地址及网络设置后重试。", Toast.LENGTH_LONG).show();}avi.hide();}@Overridepublic void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {super.onReceivedError(view, request, error);System.out.println("测试资源加载错误时进入");}@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
// Toast.makeText(MainActivity.this, "提示:hhh。"+ errorCode + "ff= " + failingUrl, Toast.LENGTH_LONG).show();isError = true;System.out.println("测试 webView页面打不开错误进入");}};/*** 双击返回桌面*/@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if ((System.currentTimeMillis() - time > 1000)) {if(isSuccess){Toast.makeText(this, "再按一次断开连接并返回连接页面", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();}time = System.currentTimeMillis();} else {if(isSuccess){webView.destroy();startActivity(new Intent(MainActivity.this, MainActivity.class));} else {
// finish();Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);startActivity(intent);}}return true;} else {return super.onKeyDown(keyCode, event);}}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.visualizedh5app"><uses-permission android:name="android.permission.INTERNET" /><applicationandroid:allowBackup="true"android:icon="@drawable/icon"android:label="@string/app_name"android:supportsRtl="true"android:usesCleartextTraffic="true"><activity android:name=".MainActivity"android:screenOrientation="landscape"android:theme="@style/AppTheme.Notitle_FullScreen"android:configChanges="orientation|keyboardHidden|screenSize"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
https://blog.csdn.net/weixin_40438421/article/details/85700109