2013-12-23 91 views
-1

Android的進度我有一個像下面的代碼:與Runnable接口

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" > 
    <ProgressBar 
     android:id="@+id/progressbar"  
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content" /> 
</LinearLayout> 

BasicViews2Activity.java

public class BasicViews2Activity extends Activity 
{  
    static int progress;  
    ProgressBar progressBar;  
    int progressStatus = 0;  
    Handler handler = new Handler(); 

    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) 
    {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);     
    progress = 0; 
    progressBar = (ProgressBar) findViewById(R.id.progressbar); 
    //---do some work in background thread--   
    new Thread(new Runnable()   
    {    
     public void run()    
     {     
     //---do some work here-- 
     while (progressStatus < 10)    
     { 
      progressStatus = doSomeWork(); 
     } 
     //---hides the progress bar--- 
     handler.post(new Runnable()   
     { 
      public void run()      
      { 
      progressBar.setVisibility(View.GONE);      
      }     
     });    
     } 

     //---do some long running work here--    
     private int doSomeWork()    
     {     
     try {  
      Thread.sleep(500); 
     } catch (InterruptedException e)     
     {      
      e.printStackTrace(); 
     }     
     return ++progress;    
     }   
    }).start();  
    } 
} 

爲什麼我們要使用兩個可運行線程類中的對象取消進度條?難道我們不能在第一個可運行的obj中執行它 - 在while循環之後,沒有另一個對象?

回答

0

asyncTask類可以很容易地用於進度條。因爲他們有一個onPreexecute和onPostexecute,我們可以通過它從後臺活動更新ui線程。

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private ProgressDialog mProgressDialog; 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_DOWNLOAD_PROGRESS: 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("waiting 5 minutes.."); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 
     return mProgressDialog; 
    default: 
    return null; 
    } 
} 

然後寫一個異步任務來更新進度..

private class DownloadZipFileTask extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

    @Override 
    protected String doInBackground(String... urls) { 
     //Copy you logic to calculate progress and call 
     publishProgress("" + progress); 
    } 

    protected void onProgressUpdate(String... progress) {   
    mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String result) {   
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
} 

希望這是你的代碼更容易...

0

@ shaon007對不起已故的帖子,是的,你可以用它做單runnable

像下面

創建XMLĴ烏斯季已創建

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" > 
    <ProgressBar 
     android:id="@+id/progressbar" 
     android:visibility="invisible" 
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content" /> 
</LinearLayout> 

的方式保持visibility選項invisible爲啓動

然後在你的mainActivity你必須做三件事那些

1)創建Handler對象
2)創建匿名runnable
3)致電run()方法從runnable與設置您的progressbar visibi lity

如下

public class MainActivity extends AppCompatActivity { 

    Handler handler; 
    private ProgressBar progressBar; 
    private int checkCount = 5000; // counter for progressbar visibility 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     progressBar = (ProgressBar) findViewById(R.id.progressbar); 

     handler = new Handler(); // handler object 

     // runnable class 
     final Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       progressBar.setVisibility(View.VISIBLE); 
       checkCount -= 1000; 
       if(checkCount > 0) { 
        handler.postDelayed(this,1000); 
       } 
       else { 
        progressBar.setVisibility(View.INVISIBLE); 
        Toast.makeText(MainActivity.this,"Progress bar invisible",Toast.LENGTH_SHORT).show(); 
        // Or your other work      
       } 
      } 
     }; 

     handler.postDelayed(runnable,1000); // will start run() method after 1 second 
    } 
} 

和你做