加载网页/ android时的进度条

时间:2012-01-06 20:17:01

标签: android

我想知道是否可以包含进度条,以便当用户按下某个按钮时 - 它会加载页面,但进度条会在页面加载之前显示。这是我的代码。

public class Sites extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.sites);

  View.OnClickListener openIt = new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(
              Intent.ACTION_VIEW,
              sites.this,   
              com.abc.example.Address.class
         ));
    }
  };

  Button sites= (Button) findViewById(R.id.siteA);
  sites.setTag(Uri.parse("http://sitea.com/")); 
  sites.setOnClickListener(openIt);     

  sites = (Button) findViewById(R.id.siteB);
  sites.setTag(Uri.parse("http://siteb.com/"));
  sites.setOnClickListener(openIt);

  sites = (Button) findViewById(R.id.siteC);
  sites.setTag(Uri.parse("https://sitec.com/"));
  sites.setOnClickListener(openIt);

  sites = (Button) findViewById(R.id.siteD);
  sites.setTag(Uri.parse("https://sited.com/"));
  sites.setOnClickListener(openIt);
}

1 个答案:

答案 0 :(得分:1)

看看AsyncTask。基本上,AsyncTask是一个模板,可以在后台加载数据,同时调用更新进度条。以下是谷歌关于如何运行AsyncTask的示例:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }