2010-01-26 23 views

Odpowiedz

45

Można użyć ProgressDialog:

ProgressDialog dialog = new ProgressDialog(this); 
dialog.setMessage("Thinking..."); 
dialog.setIndeterminate(true); 
dialog.setCancelable(false); 
dialog.show(); 

Powyższy kod pojawi się następujące okno na górze Activity:

alt text

Alternatywnie (lub dodatkowo) można wyświetlić postęp wskaźnik na pasku tytułu twojego Activity.

alt text

You need to request this as a feature pobliżu szczytu metody onCreate() swojej Activity stosując następujący kod:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 

następnie włącz go tak:

setProgressBarIndeterminateVisibility(true); 

i włącz go w następujący sposób:

setProgressBarIndeterminateVisibility(false); 
+0

Problemem jest to, że po wyświetleniu okna dialogowego Pobiegłem stosunkowo długiego leczenia, które uniemożliwiają wyświetlanie w oknie dialogowym, które pojawia się na końcu leczenie, kiedy już nie potrzebuję! – Arutha

+1

Zobacz "AsyncTask". Wyświetlasz i ukrywasz 'ProgressDialog' w' onPreExecute() 'i' onPostExecute' i wykonujesz swoją pracę na 'doInBackground'. http://android-developers.blogspot.com/2009/05/podstawowym-threading.html –

+0

Może warto też przeczytać przewodnik programisty Androida "Projektowanie pod kątem reagowania" http://developer.android.com/guide/practices/ design/responsiveness.html –

3

Oto prosty przykład to zrobić za pomocą AsyncTask:

public class MyActivity extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 

     ... 

     new MyLoadTask(this).execute(); //If you have parameters you can pass them inside execute method 

    } 

    private class MyLoadTask extends AsyncTask <Object,Void,String>{   

     private ProgressDialog dialog; 

     public MyLoadTask(MyActivity act) { 
      dialog = new ProgressDialog(act); 
     }  

     protected void onPreExecute() { 
      dialog.setMessage("Loading..."); 
      dialog.show(); 
     }  

     @Override 
     protected String doInBackground(Object... params) {   
      //Perform your task here.... 
      //Return value ... you can return any Object, I used String in this case 

      try { 
       Thread.sleep(6000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      return(new String("test")); 
     } 

     @Override 
     protected void onPostExecute(String str) {   
      //Update your UI here.... Get value from doInBackground .... 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      }   
     } 
    }