加载页面后隐藏progresbar

时间:2015-12-29 09:30:26

标签: java android eclipse webview

大家好我的progressbar有问题。 页面加载到WebView时它不会消失任何人都可以请求帮助,因为我有prbiert代码,但它不起作用 我有XML名称进度条(android:id="@+id/progressBar")我在互联网上搜索了很多解决方案,但没有任何帮助

public class GenerateQRCodeActivity extends Activity implements OnClickListener{
public static final Uri MailTo = null;
private long lastMoveEventTime = -1;
private int eventTimeInterval = 40;
private ProgressBar progressBar;
private WebView mWebView;
@SuppressLint("SetJavaScriptEnabled") 

 private String LOG_TAG = "GenerateQRCode";
protected Activity MyActivity;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  progressBar = (ProgressBar) findViewById(R.id.progressBar);
  progressBar.setMax(100);
  ImageButton button2 = (ImageButton) findViewById(R.id.button2);
  ImageButton button3 = (ImageButton) findViewById(R.id.button3);
  button2.setOnClickListener(this);
  button3.setOnClickListener(this);
  mWebView = (WebView) findViewById(R.id.webview);
  mWebView.getSettings().setJavaScriptEnabled(true);
  mWebView.setWebChromeClient(new WebChromeClient());
  WebSettings webSettings = mWebView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  mWebView.requestFocusFromTouch();
  mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); 
  mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
  mWebView.setBackgroundColor(Color.parseColor("#4dc3f1"));
  if (Build.VERSION.SDK_INT >= 11) {
      mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
  }
  mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
  mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  webSettings.setDomStorageEnabled(true);
  mWebView.setWebViewClient(new WebViewClientDemo());
  mWebView.setWebChromeClient(new WebChromeClientDemo());
  mWebView.loadUrl("http://www.google.com/");
  mWebView.getSettings().setJavaScriptEnabled(true);
  mWebView.setWebViewClient(new HelloWebViewClient());





  getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, 
          WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);





 }
 private class WebViewClientDemo extends WebViewClient {
        @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);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

         @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(0);
        }       
    }
    private class WebChromeClientDemo extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       return true;
    }

3 个答案:

答案 0 :(得分:2)

您可以使用这些代码行

      WebSettings settings = webView.getSettings();

        settings.setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        progressDialog = new ProgressDialog(ContestActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (progressDialog.isShowing() ) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(ContestActivity.this, "Error:" + description, Toast.LENGTH_SHORT).show();

            }
        });
        webView.loadUrl("YOUR URL HERE");

答案 1 :(得分:0)

中删除此行progressBar.setProgress(100);
    @Override
    public void onPageFinished(WebView view, String url)
    {
     super.onPageFinished(view, url);
     progressBar.setVisibility(View.GONE);
     progressBar.setProgress(100); // <--This line is to delete
    }

你说progressBar.setVisibality(View.GONE);隐藏进度条,之后progressBar.setProgress(100);说:再次显示:)

答案 2 :(得分:0)

您必须在Visible中将progessbar的可见性设置为onPageStarted,因此请替换此代码:

 @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.INVISIBLE);
        progressBar.setProgress(100);
    }

     @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setProgress(0);
    }   

和你一起:

  @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
        progressBar.setProgress(100);
    }

     @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.GONE);
        progressBar.setProgress(0);
    }