在加载网页时在webview中显示进度条

时间:2018-01-09 09:06:59

标签: android android-webview

我为我的网站制作了一个WebView活动,并希望添加一个加载圆形图标,如loading image,以显示网页下载的进度。 如何将这样的ProgressBar添加到我的活动中?

MainActivity.java

package club.moviestreet.www.webviewapp;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("http://webatozs.com/p/");
        // Line of Code for opening links in app
        mywebView.setWebViewClient(new WebViewClient() {

            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http://lightlobbies.com/p/")) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(i);
                    return true;
                }
                return false;
            }
        });
    }

    //Code For Back Button
    @Override
    public void onBackPressed() {
        if(mywebView.canGoBack())
        {
            mywebView.goBack();
        }

        else
        {
            super.onBackPressed();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

添加以下课程

 public class WebViewClient extends android.webkit.WebViewClient {
 @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {
 super.onPageStarted(view, url, favicon);
 }

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {  
    view.loadUrl(url);
    return true;
 }
  @Override
  public void onPageFinished(WebView view, String url) { 
    super.onPageFinished(view, url);
    // set your image from drawable folder 
    pbar.setVisibility(View.GONE); 
 }  
}

并使用onCreate()方法

进行调用
webview.setWebViewClient(new WebViewClient());

   webview.loadUrl("https://www.google.com");

答案 1 :(得分:0)

您需要覆盖更多方法onPageFinished

    package club.moviestreet.www.webviewapp;

        import android.content.Intent;
        import android.net.Uri;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.webkit.WebSettings;
        import android.webkit.WebView;
        import android.webkit.WebViewClient;

        public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    ProgressBar pbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) findViewById(R.id.webview);
        pbar = (ProgressBar)findViewById(R.id.progressBar1);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("http://webatozs.com/p/");
        // Line of Code for opening links in app
        mywebView.setWebViewClient(new WebViewClient() {

            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http://lightlobbies.com/p/")) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(i);
                    return true;
                }
                return false;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pbar.setVisibility(View.GONE);

            }
        });
    }
}

activity_main.xml ..

中粘贴xml以下
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"  >
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar1"/>
        <WebView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/webview"></WebView> 

</LinearLayout>

答案 2 :(得分:0)

以下代码将在页面成功加载后设置回调。

errors.dateOfBirth = isOverEighteen(values.dateOfBirth);