进度条

public class MainActivity extends Activity {

    private ProgressBar progress_bar;
    private TextView text_left;
    private TextView text_right;
    private int progress = 0;//记录当前进度
    private Handler handler = new Handler(){
        //这个方法就是处理接收到的消息的
        public void handleMessage(android.os.Message msg) {
            //接收消息
            if (msg.what == 1) {
                //更新界面
                text_left.setText(progress+"/100");
                text_right.setText(progress+"%");
            }
            if (msg.what == 0) {
                //取出消息内容
                int pro = (Integer) msg.obj;
                //更新界面
                text_left.setText(pro+"/100");
                text_right.setText(pro+"%");
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        progress_bar = (ProgressBar) findViewById(R.id.progress_bar);
        text_left = (TextView) findViewById(R.id.text_left);
        text_right = (TextView) findViewById(R.id.text_right);
        //模拟下载进度
        new Thread(){
            public void run() {
                while (true) {
                    if (progress < 100) {
                        progress++;
                        progress_bar.setProgress(progress);
                        try {
                            sleep(50);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        //只能通过主线程去更新界面
                  /*
                   * what:区分消息的标识
                   */
                        //handler.sendEmptyMessage(1);//马上发送一条空消息(没有具体的消息内容)  *****
                        //handler.sendEmptyMessageAtTime(what, uptimeMillis);//在指定的时间发送一条空消息
                        //delayMillis:延时的时长--以毫秒为单位
                        //handler.sendEmptyMessageDelayed(what, 3000);//延时多少秒以后,发送一条空消息 *****
                        //---------------------------------------------------------------------------
                        //1.需要创建一个Message对象
                        Message msg = Message.obtain();
                        //2.缺少消息内容,,,,还缺少区分消息的标识
                        msg.what = 0;
                        msg.obj = progress;//存放消息内容----传值
                        handler.sendMessage(msg);//马上发送一条有消息内容的消息  *****
                        //handler.sendMessageAtFrontOfQueue(msg);//在消息进入到消息队列(MessageQueue)之前发送一条有内容的消息
                        //handler.sendMessageAtTime(msg, uptimeMillis);//在指定的时间发送一条有内容的消息
                        //handler.sendMessageDelayed(msg, delayMillis);//延时多少秒以后,发送一条有内容的消息  *****
                    }else {
                        progress = 0;
                        //进行跳转
                        Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                        startActivity(intent);
                        break;
                    }
                }
            };
        }.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值