0

这是我做 Url loading 的第一堂课。

public class HelloWebViewClient extends WebViewClient
{    
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)
     {
         view.loadUrl(url);
         return true;
     }
}

这是我的活动类,其中数据在 Web 视图中被替换。

public class detailedview extends Activity
{
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailedview);
        GetSet gs = new GetSet();

        String title = gs.getTitle();
        String desc = gs.getDesc();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setTextSize(TextSize.SMALLER);
        mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
        mWebView.setWebViewClient(new HelloWebViewClient());
    }
}

我想在我的活动中放置加载数据的进度条,我尝试了很多但我做不到。

您能否编写代码或发布代码,以便我可以在根据我的代码加载数据时有一个进度条。

4

3 回答 3

1

将此代码添加到您的代码中

ProgressDialog pd = new ProgressDialog(detailedview .this);
                                       ^^^^^^^^^^^^
        pd.setMessage("Loading...");
        pd.show(); <-----

mWebView.setWebViewClient(new WebViewClient() {
^^^^^^^^    

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


public class detailedview extends Activity
{
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailedview);
        ProgressDialog pd = new ProgressDialog(detailedview .this);

        pd.setMessage("Loading...");
        pd.show(); 

       mWebView.setWebViewClient(new WebViewClient() {


@Override 
public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url);
     pd.dismiss(); 
   }
});
        GetSet gs = new GetSet();

        String title = gs.getTitle();
        String desc = gs.getDesc();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setTextSize(TextSize.SMALLER);
        mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
      //  mWebView.setWebViewClient(new HelloWebViewClient());
    }
}
于 2012-06-18T09:04:05.267 回答
0

首先让一个类说它 DownloadWebPageTask

把它放在你的 oncreate 方法中

GetSet gs = new GetSet();

    String title = gs.getTitle();
    String desc = gs.getDesc();

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setTextSize(TextSize.SMALLER);

现在像这样制作该类的对象

DownloadWebPageTask 任务 = new DownloadWebPageTask(); 任务.执行();注意:**task.execute(); 将调用你的类“DownloadWebPageTask”你可以从任何代码行开始这个现在我已经把它放在onCreate方法中

这是你的课

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

     private final ProgressDialog dialog = new ProgressDialog(Home.this);

      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Loading...");
         this.dialog.show();
      }
    @Override
    protected String doInBackground(String... urls) {
        String response = "";

      mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
         if (this.dialog.isShowing()) {
                this.dialog.dismiss();
           }

             mWebView.setWebViewClient(new HelloWebViewClient());
    }


}   

注意:**当您使用任何进度条而不是在 post excute 方法中时,所有后台进程都已完成,直到您的进度条将被显示,并且一旦数据下载完成,就会调用 onPostExcute 方法。并从外部服务器下载数据将存储在 webview 中,或者你可以用它做任何事情。如果您仍然找到任何查询,请告诉我祝您好运 Aamirkhan I。

于 2012-06-18T09:38:06.527 回答
0

您可以使用以下代码

public class Grab extends Activity {

WebView view;
private ProgressDialog progressDialog = null;


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

    setContentView(R.layout.grab);

    view = (WebView) findViewById(R.id.WebView01);
    view.setBackgroundColor(0);  // set the background transparent 

    progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER    );

    view.setWebChromeClient(new MyWebChromeClient());
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    view.loadUrl("http://www.wepware.com/web/apps/jots.do");
}


private class MyWebViewClient extends WebViewClient {
    @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)      //override WebViewClient method to open url in activity
    {
        view.loadUrl(url);
        return true;
    }
}

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if(progressDialog!=null){
            if ( newProgress >=80 ) {
                progressDialog.dismiss();
            } else {
                progressDialog.setMessage(newProgress + " % loaded");
            }
        }
        super.onProgressChanged(view, newProgress);
    }
}
}
于 2012-06-18T09:06:36.587 回答