如何在android中加载url到webview时显示进度?

如何在android中加载url到webview时显示进度?,第1张

概述我正在加载网址到webview:WebViewwebview=(WebView)findViewById(R.id.webview);webview.loadUrl(url);加载网址需要一些时间,在此期间显示空白屏幕.我想在加载网址时显示进度对话框:ProgressDialogdialog=ProgressDialog.show(this,"HI","Loading......",true);但是

我正在加载网址到webvIEw:

WebVIEw webvIEw=(WebVIEw)findVIEwByID(R.ID.webvIEw); webvIEw.loadUrl(url);

加载网址需要一些时间,在此期间显示空白屏幕.我想在加载网址时显示进度对话框:

ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true);

但是,上面的代码是行不通的.如果有任何想法,请帮助.提前致谢.

解决方法:

将WebVIEwClIEnt设置为WebVIEw,在onCreate()方法上启动进度对话框,在页面加载完onPageFinished时将其关闭(WebVIEw视图,String url)

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.ID.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, "WebVIEw 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");    }}

你的main.xml布局

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

以上是内存溢出为你收集整理的如何在android中加载url到webview时显示进度?全部内容,希望文章能够帮你解决如何在android中加载url到webview时显示进度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存