无法获取ProgressDialog停止的WebView加载后加载、ProgressDialog、WebView

2023-09-05 00:13:57 作者:一路独行也罢

我可以得到ProgressDialog显示,但我不能让它停止的WebView加载后。所有我需要的是一个简单的刷新按钮吧。

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.

下面是工作code到目前为止:

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.

推荐答案

右键这是快速的答案,你应该实现自己的类的WebViewClient,你应该表现在类的对话框,以及,但你会明白了这一点。

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.

首先,让你的对话全球,(在你的真正的应用程序,你可能想在将它传递给你的客户,或在webviewclient声明它,然后覆盖上onPageStarted以及)。

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;     

那么简单:

 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

This is the API you need: WebViewClient

修改的

确定有人讨厌我,这里的正确方法是:

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;
            }
       }
  }

新的类:

 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();
        }

 }