2012-02-16 100 views
-1

在應用程序中,當用戶點擊按鈕來運行新的活動時,它必須等待一段時間來活動加載所有內容並啓動,直到那時用戶只能看到黑屏,所以我想把進度條放到活動準備就緒。 我該怎麼做? 謝謝。使用進度條?

+0

兩個鏈接,可以幫助你:http://developer.android.com/reference/android/widget/ProgressBar.html和http://developer.android.com/guide/topics/ui/dialogs .html#ProgressDialog。請使用Google。 – talnicolas 2012-02-16 17:01:20

+0

儘量使用異步任務,我可以幫助你 – Aerrow 2012-02-16 17:23:29

回答

0

它是示例代碼,根據您的代碼進行修改。

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class ReadWebpageAsyncTask extends Activity { 
    private TextView textView; 


/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textView = (TextView) findViewById(R.id.TextView01); 
    } 

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 
      String response = ""; 
      for (String url : urls) { 
       DefaultHttpClient client = new DefaultHttpClient(); 
       HttpGet httpGet = new HttpGet(url); 
       try { 
        HttpResponse execute = client.execute(httpGet); 
        InputStream content = execute.getEntity().getContent(); 

        BufferedReader buffer = new BufferedReader(
          new InputStreamReader(content)); 
        String s = ""; 
        while ((s = buffer.readLine()) != null) { 
         response += s; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      textView.setText(result); 
     } 
    } 

    public void readWebpage(View view) { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://www.vogella.de" }); 

    } 
}