其他分享
首页 > 其他分享> > 在android中加载webview后隐藏进度条

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

作者:互联网

当我将这段代码添加到我的代码中时,我想知道什么问题.

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

进度栏和webview加载完成后,textview永远不会变得不可见

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.)web.setWebViewClient(new WebViewClient(){将覆盖先前设置的webview新的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,因为根据惯例,类必须以大写字母开头.

标签:webview,progress-bar,android
来源: https://codeday.me/bug/20191111/2022730.html