当前位置: 首页 > 知识库问答 >
问题:

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

罗智刚
2023-03-14

我正在将url加载到WebView中:

WebView webview=(WebView)findViewById(R.id.webview); 
webview.loadUrl(url);
ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true);

但是,上面的代码是不工作的。如果有任何想法,请帮助。提前谢了。

共有1个答案

章稳
2023-03-14

WebViewClient设置为WebView,在onCreate()方法上启动进度对话框,并在页面完成在onPageFinished(WebView视图,字符串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应用程序开发和学习的新手。 我已经开发了一个使用加载网站链接的应用程序。而且工作正常。 但是我在加载页面时显示和隐藏进度条时遇到了问题。 当WebView加载网站的主链接和内部链接时,我想显示一个进度条(仅在中心旋转),而在页面完全加载时,我要隐藏进度条。 我的Java代码: 还有我的布局XML代码: 现在,将显示进度条,但不会隐藏。 当我创建 进度条 对象并在Jav

  • 我有这个密码。 我正在加载这个url

  • 任何帮助都将不胜感激。

  • 我在Playstore和google send the mail上标注了一个问题,我的应用程序不安全,因为使用了SSL。 目前在我的应用程序中,我有一个,它是加载链接,并且包含https URL。 在web设置上,我是这样做的: 有没有人可以建议我如何做到这一点,使我的可以处理https url和Playstore不标记我的应用程序为不安全?

  • 我正在做一个android项目(java),在这个项目中,我想使用webview来显示一个通过使用FileChooser从Phone的存储中选择的图像。在我的java代码中,我有 欢迎任何建议。

  • 问题内容: 我正在制作一个Android应用程序,它必须通过Internet加载一些数据(只有一些数据- 并非全部)。因此,当数据正在加载且Internet连接速度很慢时,我想向用户显示“正在加载…”图标。 那我该怎么做呢?在后台加载数据时显示“正在加载…”图标,当数据完全加载后,隐藏该图标吗? 提前致谢! 问题答案: 使用异步任务来获取您的状态。