以编程方式设置进度条的进度

时间:2013-04-23 11:00:32

标签: android android-progressbar

当我的进度条从0开始时,我希望每秒都设置进度,直到达到100%。

如何做到这一点。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_dialog"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/blackie"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Envoie en cours..."
        android:textColor="@color/white"
        android:textSize="@dimen/eighteen_sp"
        android:textStyle="bold" />

    <ProgressBar
        style="@style/CustomProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dip"
        android:max="100"
        android:progress="50"
        />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

int progress = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        progress = Math.min(100, progress + 10); 
        progressBar.setProgress(progress);
        if( progress != 100 )
        {
            handler.postDelayed(this, 1000);
        }else
        { 
            handler.removeCallback(this);
         }


    }
}, 1000);

您还可以考虑为ProgressBar提供一个ID,以便您可以使用Activity方法在findViewById中访问它。

答案 1 :(得分:0)

Handler mHandler = new Handler(); 
int progress=0;

private Runnable mUpdateSpinnerTask = new Runnable() {
       public void run() {

          yourProgressBar.setProgress(progress);
           progress++
            if(progress<100)
           mHandler.postDelayed(this, 1000);

       }
    };

mHandler.postDelayed(mUpdateSpinnerTask, 1000);