2014-01-30 65 views
1

我必須在我的Android應用程序中等待幾秒鐘,並且我想在此期間顯示進度欄,我該怎麼做?在Android中顯示ProgressBar幾秒鐘

例如:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    timeForShow(5 //second); 

} 
. 
. 
. 

private void timeForShow(long mytime){ 

myprogress.setVisibility(View.VISIBLE); 
Waiting for mytime... 
myprogress.setVisibility(View.GONE); 

} 

這是我的代碼,但它不工作:

Long timerforprogressbar ; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    timerforprogressbar = (long) 5000 ; 
    new MyProgressBar().execute((Void)null); 

} 
. 

. 

. 


class MyProgressBar extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     progressbar.setVisibility(View.VISIBLE); 
     try { 
      Thread.sleep(timerforprogressbar); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     progressbar.setVisibility(View.GONE); 
    } 

} 

我的進度條:

<ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:indeterminateDrawable="@drawable/progress" > 
    </ProgressBar> 

進度是我的進度條,PLZ幫助我,TNX。

+0

我試着給一個答案,但我想你的問題的一些細節。你說這是行不通的。那是什麼做錯了?進度條沒有顯示,或者它沒有按照您的預期行事? – cYrixmorten

+0

我有一個asynctask:http://paste.ubuntu.com/6846131/。它是一個解析xml。現在,我創建了一個函數:http://paste.ubuntu.com/6846146/。我想第二次展示自己的進步(自定義時間)。但我的進步無法看到! – user3103823

+0

首先,doInBackground在後臺線程中運行,不需要有executeThread。其次,正如我在我的回答中提到的那樣,嘗試以編程方式創建progressdialog,而不是對我的答案進行編輯,稍後再演示它。 – cYrixmorten

回答

1

爲了您的具體使用情況下,它使用起來更簡單處理程序:

new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     progressbar.setVisibility(View.GONE); 
    } 

}, timerforprogressbar); 

你沒有提到你的進度是如何構建的,但如果你只是想要一個不確定的進度顯示,然後編程方式構建它會做我認爲最簡單的事情。

private ProgressDialog working_dialog; 

private void showWorkingDialog() { 
    working_dialog = ProgressDialog.show(context, "","Working please wait...", true); 
} 

private void removeWorkingDialog() { 
    if (working_dialog != null) { 
     working_dialog.dismiss(); 
     working_dialog = null; 
    } 
} 

這會使代碼看起來像:如果你在的AsyncTask doInBackground(執行任務

showWorkingDialog(); 

new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     removeWorkingDialog(); 
    } 

}, timerforprogressbar); 
0

),那麼你就可以開始在onPreExecute(你的進度)和onPostExecute禁用它( )。

0

您可以隨時使用CountDownTimer

public void timer() { 

    countDownTimer=new CountDownTimer(20000, 1000) { //display a delay of 20 seconds 

     public void onTick(long millisUntilFinished) { 


      if (somethingToBreakTheCount) 
       countDownTimer.onFinish(); 

      txtDebug.setText("seconds remaining: " + millisUntilFinished/1000); 
     } 

     public void onFinish() { 
      txtDebug.setText("Done!"); 
      finish(); 
     } 
    }.start(); 
}