如何使用android中的数据设置加载活动的进度条

时间:2012-06-18 09:00:57

标签: java android

这是我第一次上传Url的课程。

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

我想在我的活动中设置加载数据的进度条,我已经尝试了很多,但我不能这样做。

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

3 个答案:

答案 0 :(得分: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());
    }
}

答案 1 :(得分: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);
    }
}
}

答案 2 :(得分:0)

首先让一个类说下载WebPageTask

把它放在你的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 task = new DownloadWebPageTask(); task.execute(); 注:** 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我。