首页 > 移动平台 > 详细

Android-Handler更新View加线程

时间:2014-12-28 16:49:48      阅读:333      评论:0      收藏:0      [点我收藏+]

技术分享

代码

package net.blogjava.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class Main extends Activity
{
    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private Handler handler = new Handler();
    private int count1 = 0;
    private int count2 = 0;
    private Runnable doUpdateProgressBar1 = new Runnable()
    {

        @Override
        public void run()
        {

            for (count1 = 0; count1 <= progressBar1.getMax(); count1++)
            {
                handler.post(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        progressBar1.setProgress(count1);

                    }
                });
            }

        }
    };
    private Runnable doUpdateProgressBar2 = new Runnable()
    {

        @Override
        public void run()
        {
            for (count2 = 0; count2 <= progressBar2.getMax(); count2++)
            {
                handler.post(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        progressBar2.setProgress(count2);

                    }
                });
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar1 = (ProgressBar) findViewById(R.id.progressbar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressbar2);

        Thread thread1 = new Thread(doUpdateProgressBar1, "thread1");
        thread1.start();
        Thread thread2 = new Thread(doUpdateProgressBar2, "thread2");
        thread2.start();

    }
}

 

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ProgressBar android:id="@+id/progressbar1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
         style="?android:attr/progressBarStyleHorizontal" android:max="20000"
        android:layout_marginTop="10dp" />
    <ProgressBar android:id="@+id/progressbar2"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal" android:max="10000"
        android:layout_marginTop="10dp"  />
</LinearLayout>

 

Android-Handler更新View加线程

原文:http://www.cnblogs.com/spadd/p/4190041.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!