2012-01-06 117 views
0

我想知道是否可以包含進度條,以便當用戶按下某個按鈕時 - 它加載頁面,但進度條在頁面顯示之前顯示加載。這是我的代碼。加載網頁時的進度條/ 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

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