WebView 加载后无法让 ProgressDialog 停止

发布于 2024-11-18 02:03:44 字数 936 浏览 3 评论 0原文

我可以显示 ProgressDialog,但在加载 WebView 后无法让它停止。我所需要的只是一个简单的刷新按钮。

这是到目前为止的工作代码:

      public class Quotes extends Activity implements OnClickListener{

      WebView webview;

      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      webview = (WebView) findViewById(R.id.scroll);
      webview.getSettings().setJavaScriptEnabled(true);
      webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html");

      View refreshClick = findViewById(R.id.refresh);
      refreshClick.setOnClickListener(this);

      }


      public void onClick(View v){
           switch(v.getId()){

           case R.id.refresh:
                ProgressDialog.show(Quotes.this, "", "Loading...", true);
                webview.reload();

                }
           }
      }

我只是想不通。我到处都看过,但似乎没有任何效果。

I can get the ProgressDialog to show, but I cannot get it to stop after the WebView has been loaded. All I need is a simple refresh button for it.

Here is the working code so far:

      public class Quotes extends Activity implements OnClickListener{

      WebView webview;

      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      webview = (WebView) findViewById(R.id.scroll);
      webview.getSettings().setJavaScriptEnabled(true);
      webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html");

      View refreshClick = findViewById(R.id.refresh);
      refreshClick.setOnClickListener(this);

      }


      public void onClick(View v){
           switch(v.getId()){

           case R.id.refresh:
                ProgressDialog.show(Quotes.this, "", "Loading...", true);
                webview.reload();

                }
           }
      }

I just can't figure it out. I've looked everywhere but nothing seems to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-11-25 02:03:44

是的,这是快速答案,您应该真正为 WebViewClient 实现您自己的类,您也应该在该类中显示对话框,但您会弄清楚的。

首先,使您的对话框全局化(在您的实际应用程序中,您可能希望将其传递给您的客户端,或在 webviewclient 中声明它,然后也覆盖 onPageStarted)。

  ProgressDialog dialog;     

然后简单地说:

 webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dialog.dismiss();
        }
    });



   public void onClick(View v){
       switch(v.getId()){

       case R.id.refresh:
            dialog = ProgressDialog.show(Quotes.this, "", "Loading...", true);
            webview.reload();

            }
       }
  }

这是您需要的API: WebViewClient

编辑

好的这让我很恼火,这是正确的方法:

 public class Quotes extends Activity implements OnClickListener{

  private WebView webview;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webview = (WebView) findViewById(R.id.scroll);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new MyWebViewClient(this));
    webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html");

    findViewById(R.id.refresh).setOnClickListener(this);
  }


  public void onClick(View v){
       switch(v.getId()){
       case R.id.refresh:
            webview.reload();
             break;
            }
       }
  }

新课程:

 public class MyWebViewClient extends WebViewClient {
    private Context mContext;
    private ProgressDialog mDialog;

  public MyWebViewClient(Context context){
    mContext = context;
  }

  @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mDialog = ProgressDialog.show(mContext, "", "Loading...", true);
        }

    @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mDialog.dismiss();
        }

 }

Right this is the quick answer, you should really implement your own class for the WebViewClient, you should show the dialog in that class as well but you'll figure that out.

First, make your dialog global, (in your real app you might want to pass it in to your client, or declare it in the webviewclient, then override on onPageStarted as well).

  ProgressDialog dialog;     

then simply:

 webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dialog.dismiss();
        }
    });



   public void onClick(View v){
       switch(v.getId()){

       case R.id.refresh:
            dialog = ProgressDialog.show(Quotes.this, "", "Loading...", true);
            webview.reload();

            }
       }
  }

This is the API you need: WebViewClient

EDIT

Ok it was annoying me, here is the proper way:

 public class Quotes extends Activity implements OnClickListener{

  private WebView webview;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webview = (WebView) findViewById(R.id.scroll);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new MyWebViewClient(this));
    webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html");

    findViewById(R.id.refresh).setOnClickListener(this);
  }


  public void onClick(View v){
       switch(v.getId()){
       case R.id.refresh:
            webview.reload();
             break;
            }
       }
  }

The new class:

 public class MyWebViewClient extends WebViewClient {
    private Context mContext;
    private ProgressDialog mDialog;

  public MyWebViewClient(Context context){
    mContext = context;
  }

  @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mDialog = ProgressDialog.show(mContext, "", "Loading...", true);
        }

    @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mDialog.dismiss();
        }

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文