按下按钮重新加载WebView ?(安卓工作室)



我在我的Android Studio项目中有一个WebView,它填满了整个屏幕,当他们进入没有互联网连接的应用程序时,我显示我包含的一个活动,我隐藏WebView,我显示文本和一个按钮,告诉他们他们没有互联网。我如何让按钮重新加载WebView时,它被按下?重新加载整个应用程序也可以,但作为最后的手段。

我的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:background="@color/white"
android:backgroundTint="@color/white"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
</WebView>
<LinearLayout
android:id="@+id/no_internet_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/no_internet"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

下面是我的no-internet.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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="54dp"
android:fontFamily="sans-serif-medium"
android:text="Sorry (user)!"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063"
tools:layout_editor_absoluteX="0dp" />
<TextView
android:id="@+id/errorMessage"
android:layout_width="match_parent"
android:layout_height="34dp"
android:text="Something went wrong with this app."
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@+id/space"
tools:layout_editor_absoluteX="0dp" />
<TextView
android:id="@+id/errorCode"
android:layout_width="match_parent"
android:layout_height="22dp"
android:text="ERROR_INTERNET_DISCONECTED"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/errorMessage"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/refreshButton"
android:layout_width="127dp"
android:layout_height="59dp"
android:text="Retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.854"
app:strokeColor="@color/blue_primary" />
<Space
android:id="@+id/space"
android:layout_width="237dp"
android:layout_height="37dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的mainactivity。java文件:

package com.example.projectname;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
LinearLayout no_internet_view = (LinearLayout) findViewById(R.id.no_internet_view);//////this line for define no_internet_view
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webView.loadUrl("https://websitename.com");
webView.setWebViewClient(new WebViewClient());
if (isInternetOn()) {
webView.setVisibility(View.VISIBLE);
no_internet_view.setVisibility(View.GONE);
} else {
webView.setVisibility(View.GONE);
no_internet_view.setVisibility(View.VISIBLE);
}
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed(); //maybe shove option first later
}
}
public boolean isInternetOn() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
}

我的reloadButton将会重载页面。

我假设您正在使用Java。我给你做了一个榜样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("url");
Button button = (Button) findViewById(R.id.reloadButton);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

webview.reload();

}
});
}

当按钮被按下时,webview重新加载。

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

webview.loadUrl("url");

}
});

最新更新