4

我想这样做,进度对话框等待webview上的加载项。我该怎么做dialog.dismiss()取决于在webview上加载项目的事件?

    public class asynctask extends  AsyncTask<Void, Void, Void> {

    private ProgressDialog dialog = new ProgressDialog(WebActivity.this);


  @Override
  protected void onPostExecute(Void result) {
      // TODO Auto-generated method stub

在那里我该怎么做??

      dialog.dismiss();

对话框消失,无需等待

     }
 @Override 
 protected void onPreExecute() {
      // TODO Auto-generated method stub
        dialog.setMessage("Loading..Please wait.");

        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
     }



@Override
protected Void doInBackground(Void... params) {
      // TODO Auto-generated method stub




      return null;
     }


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);


    Bundle extras = getIntent().getExtras();
    String url=extras.getString("adres");

    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(url);



    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    new asynctask().execute();

}

4

1 回答 1

20

不要使用 AsyncTask,因为你不负责加载 webview。如果你想显示一个进度对话框,这里是如何做到的。

private ProgressDialog dialog = new ProgressDialog(WebActivity.this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    webView = (WebView) findViewById(R.id.webView1);

    Bundle extras = getIntent().getExtras();
    String url=extras.getString("adres");

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {                  
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    });
    dialog.setMessage("Loading..Please wait.");
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
    webView.loadUrl(url);



    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
}

这个想法是您显示对话框,开始加载 url,当 web 客户端看到页面已完成加载时,它会关闭对话框。

于 2013-01-02T13:19:54.860 回答