展会信息港展会大全

android笔记之在WebView中显示ProgressBar的两种方法
来源:互联网   发布日期:2015-10-03 10:55:44   浏览:1723次  

导读:,这种方法只能将进度情况显示到标题栏中。英文是本文作者的一段唠叨,很简单,我就不用再翻译了吧。When using the webview, something that drives me crazy, specially if you are in a place......

,这种方法只能将进度情况显示到标题栏中。英文是本文作者的一段唠叨,很简单,我就不用再翻译了吧。

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

// 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);

}

});

// 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);

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.

<?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>

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");

}

}

赞助本站

人工智能实验室

相关热词: WebView ProgressBar

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港