Android web与progressbar结合使用

来源:互联网 发布:linux cp创建文件夹 编辑:程序博客网 时间:2024/06/03 14:28

xml文件:

<FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" >


            <WebView
                android:id="@+id/webView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />


            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="gone" />
        </FrameLayout>



java代码:



WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl(AppClient.BASE_URL);





final ProgressBar progressBar=(ProgressBar) findViewById(R.id.progressBar);
webView.setWebViewClient(new WebViewClient(){
//网页加载开始时调用,显示加载提示旋转进度条
           public void onPageStarted(WebView view, String url, Bitmap favicon) {
               // TODO Auto-generated method stub
               super.onPageStarted(view, url, (android.graphics.Bitmap) favicon);
               progressBar.setVisibility(android.view.View.VISIBLE);
//                Toast.makeText(ElecHall.this, "onPageStarted", 2).show();
           }


//网页加载完成时调用,隐藏加载提示旋转进度条
           @Override
           public void onPageFinished(WebView view, String url) {
               // TODO Auto-generated method stub
               super.onPageFinished(view, url);
               progressBar.setVisibility(android.view.View.GONE);
//                Toast.makeText(ElecHall.this, "onPageFinished", 2).show();
           }
//网页加载失败时调用,隐藏加载提示旋转进度条
           @Override
           public void onReceivedError(WebView view, int errorCode,
                   String description, String failingUrl) {
               // TODO Auto-generated method stub
               super.onReceivedError(view, errorCode, description, failingUrl);
               progressBar.setVisibility(android.view.View.GONE);
//                Toast.makeText(ElecHall.this, "onReceivedError", 2).show();
           }
           
       });

0 0