简体   繁体   English

加载网页/ android时的进度条

[英]progress bar while loading web page/android

I would like to know if it is possible to include a progress bar, so that when a user presses the certain button -- it loads the page but the progress bar shows before the page is loaded. 我想知道是否可以包含进度条,以便当用户按下某个按钮时-它会加载页面,但是进度条会在加载页面之前显示。 here is my code. 这是我的代码。

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

Look at AsyncTask . 看一下AsyncTask Basically, AsyncTask is a template that loads in data in the background, while having a call to update a progress bar. 基本上,AsyncTask是一个模板,它在调用更新进度条的同时在后台加载数据。 Here is the example from google on how to run an 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");
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM