简体   繁体   English

没有互联网时,如何使我的按钮消失?

[英]How can I make my button in disappear when there's no internet?

Hello I'm trying to get my button that I have in my main XML file to only appear when there's no internet So that's why I can get the button to only appear when there's no internet and so it's not shown anywhere else unless there's internet can someone please help me?您好,我正在尝试让我的主 XML 文件中的按钮仅在没有互联网时出现所以这就是为什么我可以让按钮仅在没有互联网时出现,因此除非有互联网,否则它不会显示在其他任何地方有人请帮帮我吗?

Blow i have listed the code for both the main XML and the main Java file吹我已经列出了主要 XML 和主要 Java 文件的代码

xml xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <WebView
        android:layout_width="10dp"
        android:layout_height="10dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="200dp"
        tools:layout_editor_absoluteY="361dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="202dp"
        tools:layout_editor_absoluteY="399dp" />


</androidx.constraintlayout.widget.ConstraintLayout>\

java java

public class MainActivity extends AppCompatActivity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = new WebView(this);
        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
        setContentView(webView);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);


        if(isNetworkAvailable(this)){

            webView.loadUrl("https://racks.tk");

        } else {


            Toast.makeText(this,"No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();

        }
    }

    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
                return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            } else {
                NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
                return activeNetwork != null && activeNetwork.isConnected();

            }
        }

        return false;
    }

Use the setVisibility() method使用setVisibility()方法

Button btn = findViewById(R.id.button);

if(isNetworkAvailable(this)){

            webView.loadUrl("https://racks.tk");

            btn.setVisibility(View.VISIBLE)

        } else {

            btn.setVisibility(View.GONE)
            Toast.makeText(this,"No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();

        }

You are missing the @Override :您缺少@Override

Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override //Right here
        public void onClick(View v) {
            webView.loadUrl( "javascript:window.location.reload( true )" );
        }
    });

You are creating a web view programmatically and then trying to access a button from an XML file and you have not linked the XML file in the Java file, so it crashes. You are creating a web view programmatically and then trying to access a button from an XML file and you have not linked the XML file in the Java file, so it crashes. Update your code with this code completely使用此代码完全更新您的代码

XML file - activity_main.xml XML 文件-activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ProgressBar
        android:id="@+id/my_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true"/>

    <WebView
        android:id="@+id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:id="@+id/reload_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Retry" />


</RelativeLayout>

java file - MainActivity.java java 文件 - MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView webView = findViewById(R.id.my_webview);
        Button reloadBtn = findViewById(R.id.reload_btn);
        ProgressBar progress = findViewById(R.id.my_progress);

        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);


        webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                webView.setVisibility(View.GONE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progress.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });


        if (isNetworkAvailable(this)) {

            webView.loadUrl("https://racks.tk");

            reloadBtn.setVisibility(View.GONE);

        } else {

            Toast.makeText(this, "No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();

            reloadBtn.setVisibility(View.VISIBLE);

        }


        reloadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isNetworkAvailable(MainActivity.this)){
                    reloadBtn.setVisibility(View.GONE);
                    webView.loadUrl("https://racks.tk");
                }else {

                    reloadBtn.setVisibility(View.VISIBLE);
                    Toast.makeText(MainActivity.this, "No internet. Please check internet connection and try again", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    public static boolean isNetworkAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
                return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            } else {
                NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
                return activeNetwork != null && activeNetwork.isConnected();

            }
        }

        return false;
    }
}

Button button= findViewById(R.id.button);按钮按钮= findViewById(R.id.button);

if(isNetworkAvailable(this)){ if(isNetworkAvailable(this)){

        webView.loadUrl("myurl.com");

        button.setVisibility(View.VISIBLE)

    } else {

        button.setVisibility(View.GONE)
        Toast.makeText(this,"check internet connection and try again", Toast.LENGTH_LONG).show();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android中单击按钮时如何使光标消失 - how can I make curser disappear when button is clicked in android 单击图像时如何使图像消失? - How can I make the Image disappear when I click into it? 如何让我的ActionBar的“向上”按钮像手机的“后退”按钮一样工作? - How can I make my ActionBar's “Up” button work like the phone's “Back” button? 我如何使按钮出现和消失? - How do I make a button appear and disappear?Libgdx 在RecyclerView中滚动时,如何使RelativeLayout消失 - How do I make RelativeLayout disappear when scrolling in a RecyclerView 我的按钮不工作,我怎样才能让它在 android studio 上工作? - My button is not working, how can i make it work on android studio? 如何使我的Android应用程序的按钮时尚 - How can I make the button stylish for my android app 如何使我的JTable在单击按钮时出现? - How can I make my JTable appear on button click? 如何使浮动按钮消失android? - How to make a floating button disappear android? 当向其添加actionListener时,为什么我的按钮从我的borderlayout中消失了? - Why does my button disappear from my borderlayout when I add an actionListener to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM