在android中加载webview后隐藏进度条

时间:2017-01-13 16:52:22

标签: android webview progress-bar

我想知道当我将这段代码添加到我的代码时会出现什么问题。

  web.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
       web.loadUrl("file:///android_asset/error.html");
   }});

Progressbar& textview加载完成后,webview永远不会隐藏

public class YazdMap extends ActionBarActivity {

WebView web;
ProgressBar progressBar;
TextView txt;


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

    web = (WebView) findViewById(R.id.webview1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    txt = (TextView) findViewById(R.id.txt);



    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://google.com/");



   web.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
       web.loadUrl("file:///android_asset/error.html");
 }
});


}

public class myWebClient extends WebViewClient
{

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(view.GONE);
        txt.setVisibility(view.GONE);

    }

    @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 boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
    {web.goBack();
    return true;}

    return super.onKeyDown(keyCode, event);
}

}

1 个答案:

答案 0 :(得分:3)

1。)web.setWebViewClient(new WebViewClient() {将覆盖之前设置的网络视图new myWebClient(),因此请删除此

web.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
       web.loadUrl("file:///android_asset/error.html");
 }
});

2。)将onReceivedError移到myWebClient课程中,以便代码

public class YazdMap extends ActionBarActivity {

    WebView web;
    ProgressBar progressBar;
    TextView txt;


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

        web = (WebView) findViewById(R.id.webview1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        txt = (TextView) findViewById(R.id.txt);

        web.setWebViewClient(new MyWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://google.com/");

    }

    public class MyWebClient extends WebViewClient
    {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(view.GONE);
                txt.setVisibility(view.GONE);

            }

            @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 onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               view.loadUrl("file:///android_asset/error.html");
            }

        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
        {web.goBack();
        return true;}

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

注意:我将名称从myWebClient更改为MyWebClient,因为类必须按照惯例将首字母作为大写字母。