最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

Android 在WebView中显示ProgressBar的两种方法

Android Dev ajiang-tuzi 4215浏览

第一种方法是用系统的资源,这种方法只能将进度情况显示到标题栏中

When using the webview, something that drives me crazy, specially if you are in a place with a very slow internet connection, is not knowing what is happening with the webpage, is it loading? Is it stuck? …. AAAhhhh Nothing is happening. Ok don’t desperate, I am going to show you how to add a progress bar to your Webview layout in you app.
After you know what to do is easy (As always).

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
      // Sets the Chrome Client, and defines the onProgressChanged  
// This makes the Progress bar be updated.  
final Activity MyActivity = this;  
mWebView.setWebChromeClient(new WebChromeClient() {  
 public void onProgressChanged(WebView view, int progress)     
 {  
  //Make the bar disappear after URL is loaded, and changes string to Loading...  
  MyActivity.setTitle("Loading...");  
  MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded  
   
  // Return the app name after finish loading  
     if(progress == 100)  
        MyActivity.setTitle(R.string.app_name);  
   }  
 });  
 


·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
          // Adds Progrss bar Support  
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);  
setContentView(R.layout.main );  
   
// Makes Progress bar Visible  
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);  
 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
package firsrdroid.tutorial.mywebview;  
   
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Window;  
import android.webkit.WebChromeClient;  
import android.webkit.WebView;  
   
public class UsingMyWebview extends Activity {  
      
   WebView mWebView;  
      
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState)   
   {  
      super.onCreate(savedInstanceState);  
         
      // Adds Progrss bar Support  
      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);  
      setContentView(R.layout.main );  
         
      // Makes Progress bar Visible  
      getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);   
   
       // Get Web view  
       mWebView = (WebView) findViewById( R.id.MyWebview ); //This is the id you gave   
                                              //to the WebView in the main.xml  
       mWebView.getSettings().setJavaScriptEnabled(true);     
       mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this   
                                              //if ROM supports Multi-Touch        
       mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM  
         
       // Load URL  
       mWebView.loadUrl("http://www.firstdroid.com/advertisement.htm");  
          
          
       // Sets the Chrome Client, and defines the onProgressChanged  
       // This makes the Progress bar be updated.  
       final Activity MyActivity = this;  
       mWebView.setWebChromeClient(new WebChromeClient() {  
        public void onProgressChanged(WebView view, int progress)     
        {  
         //Make the bar disappear after URL is loaded, and changes string to Loading...  
         MyActivity.setTitle("Loading...");  
         MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded  
   
         // Return the app name after finish loading  
            if(progress == 100)  
               MyActivity.setTitle(R.string.app_name);  
          }  
        });  
          
          
          
   }//End of Method onCreate  
}  
 

下面是第二种方法:,和上面的方法不同的是,这个是进度没有显示在标题栏而是显示在页面的上面。

I’ve seen a multitude of posts on how to embed the Android WebView object and how to use the built-in feature request PROGRESS, but I’ve yet to come across a demonstration on just how simple it is to integrate a ProgressDialog object. I specifically needed this for one of my projects because I did not want the title bar to render in my application. If the title is not rendered, then the Window Feature Request progress bar and indeterminant functions will not render at all.

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <WebView android:id="@string/webview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_weight="1" />  
</LinearLayout>  
 
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
package com.maxpowersoft.example;  
   
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.Window;  
import android.webkit.WebSettings;  
import android.webkit.WebView;  
import android.webkit.WebViewClient;  
import android.widget.Toast;  
   
public class Main extends Activity {  
    private WebView webview;  
    private static final String TAG = "Main";  
    private ProgressDialog progressBar;    
   
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
   
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
   
        setContentView(R.layout.main);  
   
        this.webview = (WebView)findViewById(R.string.webview);  
   
        WebSettings settings = webview.getSettings();  
        settings.setJavaScriptEnabled(true);  
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);  
   
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
   
        progressBar = ProgressDialog.show(Main.this, "MaxPowerSoft Example", "Loading...");  
   
        webview.setWebViewClient(new WebViewClient() {  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                Log.i(TAG, "Processing webview url click...");  
                view.loadUrl(url);  
                return true;  
            }  
   
            public void onPageFinished(WebView view, String url) {  
                Log.i(TAG, "Finished loading URL: " +url);  
                if (progressBar.isShowing()) {  
                    progressBar.dismiss();  
                }  
            }  
   
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {  
                Log.e(TAG, "Error: " + description);  
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();  
                alertDialog.setTitle("Error");  
                alertDialog.setMessage(description);  
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                        return;  
                    }  
                });  
                alertDialog.show();  
            }  
        });  
        webview.loadUrl("http://www.google.com");  
    }  
}  

转载请注明:(●--●) Hello.My Weicot » Android 在WebView中显示ProgressBar的两种方法

蜀ICP备15020253号-1