斩月也会过期
2015-05-21 02:58
采纳率: 66.7%
浏览 1.5k

关于这段ProgressBar的代码看不懂

package com.example.progressbartest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.widget.ProgressBar;

public class MainActivity extends ActionBarActivity {

private int[] data = new int[100];
int hasData = 0;
int status = 0;
ProgressBar bar, bar2;

Handler mHandler = new Handler() {

    public void handleMessage(Message msg) {

        if (msg.what == 0x111) {
            bar.setProgress(status);
            bar2.setProgress(status);
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bar=(ProgressBar) findViewById(R.id.bar);
    bar2=(ProgressBar) findViewById(R.id.bar2);
    new Thread()
    {
            public void run() {
        while(status<100){
            status=doWork();
            mHandler.sendEmptyMessage(0x111);
        }   
            }
            }.start();

    }
    public int doWork(){

        data[hasData++]=(int)(Math.random()*100);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

            e.printStackTrace();

        }
        return hasData;
}

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • danielinbiti 2015-05-21 03:18
    已采纳
     Handler mHandler = new Handler() {
    
        public void handleMessage(Message msg) {
    
            if (msg.what == 0x111) {//消息类型,与sendEmptyMessage(0x111)中传输的消息类型0x111是一致的
                bar.setProgress(status);//根据随机的数目更新进度条
                bar2.setProgress(status);
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bar=(ProgressBar) findViewById(R.id.bar);
        bar2=(ProgressBar) findViewById(R.id.bar2);
        new Thread()
        {
                public void run() {
            while(status<100){
                status=doWork();//产生一百个随机数个数
                mHandler.sendEmptyMessage(0x111);//因为在thread中是无法直接更新UI的,必须在UI现场中才能更新,所以通过handler发送消息,该消息由handler的handleMessage接收
            }   
                }
                }.start();
    
        }
        public int doWork(){
    
            data[hasData++]=(int)(Math.random()*100);  
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
                e.printStackTrace();
    
            }
            return hasData;
    }
    
    打赏 评论
  • java-缘 2015-05-24 03:03

    代码能多点吗?没懂呢恩

    打赏 评论

相关推荐 更多相似问题