我想在加载网站时显示进度文本

时间:2016-08-26 08:22:43

标签: android webview progress-bar

这是我的第二项活动 任何人都可以帮助我在加载网站时添加进度文本,加载后它应该停止,帮我把代码放在正确的位置,因为我不太了解java,我只是在学习

package com.example.jaisonjoseph.newsclient;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.support.design.widget.NavigationView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

公共类Manorama扩展了Activity {

Toolbar toolbar=null;
public WebView mwebView;

    public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)  
     context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null)
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected())
                return true;
    }
    return false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manorama);


    if(isNetworkStatusAvialable (getApplicationContext())) {
        WebView view=(WebView) findViewById(R.id.webView2);
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new WebViewClient());


        view.loadUrl("http://manoramaonline.com");
    } else {
        WebView view1=(WebView) findViewById(R.id.webView2);
        view1.getSettings().setJavaScriptEnabled(true);
        view1.setWebViewClient(new WebViewClient());
        view1.loadUrl("file:///android_asset/index.html");
    }
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction()==KeyEvent.ACTION_DOWN)        {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                WebView webView = (WebView) findViewById(R.id.webView2);
                if (webView != null) {


                    if (webView.canGoBack()) {
                        webView.goBack();
                    } else {
                        finish();
                    }
                    return true;
                }

        }
    }
    return super.onKeyDown(keyCode, event);
  }


private class MyWebViewClient extends WebViewClient {
    ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar);

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

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

1 个答案:

答案 0 :(得分:2)

您应该设置WebViewClient以启动和完成监听页面加载。你可以这样做:

自定义WebViewClient:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

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

将上述客户端设置为您的webView:

webView.setWebViewClient(new MyWebViewClient());

您还可以在ProgressBar下方添加TextView并设置其可见性。祝你好运。