android筆記之在WebView中顯示ProgressBar的兩種方法

來源:互聯網
上載者:User

第一種方法是用系統的資源,這種方法只能將進度情況顯示到標題列中。英文是本文作者的一段嘮叨,很簡單,我就不用再翻譯了吧。

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).     

// Sets the Chrome Client, and defines the onProgressChanged<br />// This makes the Progress bar be updated.<br />final Activity MyActivity = this;<br />mWebView.setWebChromeClient(new WebChromeClient() {<br /> public void onProgressChanged(WebView view, int progress)<br /> {<br /> //Make the bar disappear after URL is loaded, and changes string to Loading...<br /> MyActivity.setTitle("Loading...");<br /> MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded</p><p> // Return the app name after finish loading<br /> if(progress == 100)<br /> MyActivity.setTitle(R.string.app_name);<br /> }<br /> });<br /> 

          // Adds Progrss bar Support<br />this.getWindow().requestFeature(Window.FEATURE_PROGRESS);<br />setContentView(R.layout.main );<br /> <br />// Makes Progress bar Visible<br />getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

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

下面是第二種方法:英文是本文作者的一段嘮叨,也很簡單,我就不用再翻譯了吧。和上面的方法不同的是,這個是進度沒有顯示在標題列而是顯示在頁面的上面。

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.

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <WebView android:id="@string/webview"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1" /><br /></LinearLayout> 

package com.maxpowersoft.example;</p><p>import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.app.ProgressDialog;<br />import android.content.DialogInterface;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.util.Log;<br />import android.view.Window;<br />import android.webkit.WebSettings;<br />import android.webkit.WebView;<br />import android.webkit.WebViewClient;<br />import android.widget.Toast;</p><p>public class Main extends Activity {<br /> private WebView webview;<br /> private static final String TAG = "Main";<br /> private ProgressDialog progressBar; </p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> requestWindowFeature(Window.FEATURE_NO_TITLE);</p><p> setContentView(R.layout.main);</p><p> this.webview = (WebView)findViewById(R.string.webview);</p><p> WebSettings settings = webview.getSettings();<br /> settings.setJavaScriptEnabled(true);<br /> webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);</p><p> final AlertDialog alertDialog = new AlertDialog.Builder(this).create();</p><p> progressBar = ProgressDialog.show(Main.this, "MaxPowerSoft Example", "Loading...");</p><p> webview.setWebViewClient(new WebViewClient() {<br /> public boolean shouldOverrideUrlLoading(WebView view, String url) {<br /> Log.i(TAG, "Processing webview url click...");<br /> view.loadUrl(url);<br /> return true;<br /> }</p><p> public void onPageFinished(WebView view, String url) {<br /> Log.i(TAG, "Finished loading URL: " +url);<br /> if (progressBar.isShowing()) {<br /> progressBar.dismiss();<br /> }<br /> }</p><p> public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {<br /> Log.e(TAG, "Error: " + description);<br /> Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();<br /> alertDialog.setTitle("Error");<br /> alertDialog.setMessage(description);<br /> alertDialog.setButton("OK", new DialogInterface.OnClickListener() {<br /> public void onClick(DialogInterface dialog, int which) {<br /> return;<br /> }<br /> });<br /> alertDialog.show();<br /> }<br /> });<br /> webview.loadUrl("http://www.google.com");<br /> }<br />} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.