我想在 webview 中隐藏 url(当没有互联网时)并显示我自己的文本

i want to hide the url in webview (when there is no internet ) and display my own text

[

我想在没有 Internet 连接时显示我自己的文本以向用户隐藏网页的 url。检查 link 的屏幕截图。我只想隐藏 link 是否可能

设置 WebViewClient 并在 onReceivedError() 中侦听错误,隐藏您的 webview 并在另一个 中显示错误]textview.

webView.setWebViewClient(new WebViewClient(){

@Override public void onReceivedError(WebView view, WebResourceRequest request,
      WebResourceError error) {
    super.onReceivedError(view, request, error);
    // hide webview and show error textview
  }
});

首先,检查这个互联网

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

然后

mainWebView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
             String summary =
             "<html><body>Could not connect to the server.</body></html>";
             mainWebView.loadData(summary, "text/html", null);

            Toast.makeText(activity, "" + description, Toast.LENGTH_SHORT)
                    .show();
        }
    });

    webview.loadUrl("http://www.edupointbd.com/");

不要忘记添加清单文件

<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"/>

Full Source

java 文件这将在应用程序启动时检查互联网 运行

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class isNetworkAvailable extends Activity {
    private ImageButton btn;
private Handler h = new Handler();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_is_network);
            btn = (ImageButton) findViewById(R.id.bu);
   btn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
                     restar();
       }
   });
                    if (!isNetworkAvailable()) {
                        //Create an alert dialog


                        AlertDialog.Builder Checkbuilder = new AlertDialog.Builder(isNetworkAvailable.this);
                        Checkbuilder.setIcon(R.drawable.error);
                        Checkbuilder.setTitle("Error!");
                        Checkbuilder.setMessage("Check Your Internet Connection.");
                        //Builder Retry Button

                        Checkbuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                //Restart The Activity
                                Intent intent = getIntent();
                                finish();
                                startActivity(intent);

                            }
                        });


                Checkbuilder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });

                AlertDialog alert = Checkbuilder.create();
                alert.show();

            } else {
                if (isNetworkAvailable()) {

                    Thread tr = new Thread() {
                        public void run() {
                            try {
                                sleep(4);
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                Intent i = new Intent(isNetworkAvailable.this, MainActivity.class);
                                startActivity(i);

                                finish();
                            }
                        }
                    };
                    tr.start();

                }
            }

        }


        private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null;

        }

public  void restar(){

            Intent intenet =new Intent(this,isNetworkAvailable.class);

                    startActivity(intenet);
                    finish();
        }
    }

上面java代码的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:background="@android:color/white"
    tools:context=".isNetworkAvailable">


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="389dp"
        android:layout_height="439dp"
        android:contentDescription="no internet check your connection"
        android:src="@raw/no"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/bu"
        android:layout_width="335dp"
        android:layout_height="80dp"
        android:layout_below="@id/imageView2"
        android:layout_marginTop="32dp"
        android:contentDescription="Tap to retry"

        android:src="@raw/r"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</android.support.constraint.ConstraintLayout>

然后将这行代码添加到带有 webview 的片段中(这将在 webview 开始加载时检查互联网

 mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                mWebView.loadUrl("file:///android_asset/error.html");

你的 android 主要内容应该是这样的

<application
        android:icon="@mipmap/ic_app"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_app_round"
        android:supportsRtl="true"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity
            android:name=".isNetworkAvailable"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">


            <intent-filter>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

        </activity>

将此文件放在名称为 error.html

的资产下
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <title>No Connection</title>

    <!-- Stylesheets-->
    <style type="text/css">
 body{
  background: #E1e1e1;
}

#cloud{
  width: 300px;
  height: 120px;
  background: #676767;

  background: -webkit-linear-gradient(-90deg,#676767 5%, #676767 100%);

  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  border-radius: 100px;

  position: relative;

  margin: 150px auto 0;
  opacity: .5;
}

#cloud:before, #cloud:after{
  content: '';
  position:absolute;
  background: #676767;
  z-index: -1;
}

#cloud:after{
  width: 100px;
  height: 100px;
  top: -50px;
  left:50px;

  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  border-radius: 100px;
}

#cloud:before{
  width: 120px;
  height: 120px;
  top: -70px;
  right: 50px;

  -webkit-border-radius: 200px;
  -moz-border-radius: 200px;
  border-radius: 200px;
}

.shadow {
  width: 300px;
  position: absolute;
  bottom: -10px;
  background: black;
  z-index: -1;

  -webkit-box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);
  -moz-box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);
  box-shadow: 0 0 25px 8px rgba(0,0,0,0.4);

  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;

}
h1{
  color: #fff;
  font-size: 25px;
  padding-top: 1px;
  text-align: center ;
  margin: 2px auto;
}

h2 {
  color: #fff;
  font-size: 17px;
  padding-top: 15px;
  text-align: center;
  margin: 5px auto;
}

h4 {
  color: #fff;
  font-size: 14px;
  margin: 0 auto;
  padding: 0;
  text-align: center;
}

 </style>

<body>
<div id="cloud"><h1>Whoops!</h1>
    <h2>Slow or No internet Connection :(</h2>
    <h4>Please Check your WiFi or Mobile Internet!</h4>
    <span class="shadow"></span></div>

</body>
</html>

您需要输入以上所有代码才能正常工作。希望这会对你有所帮助