您的位置:首页 > 移动开发 > Android开发

handler实例,progressbar

2011-10-13 02:14 232 查看
xml

<?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/bar"

    android:layout_width="200dip"

    android:layout_height="wrap_content"

    style="?android:attr/progressBarStyleHorizontal"

    android:visibility="gone"

    />

<Button

    android:id="@+id/startButton"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="start"

    />

</LinearLayout>

activity中的代码

package com.ph;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ProgressBar;

public class ProgressbarHandlerActivity extends Activity {

    ProgressBar bar = null;

    Button startButton = null;

    

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        bar = (ProgressBar) findViewById(R.id.bar);

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new OnClickListener(){

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                bar.setVisibility(View.VISIBLE);

                updateBarHandler.post(updateThread);

            }

            

        });

    }

    

    //使用匿名内部类来复写handleMessage方法

    Handler updateBarHandler = new Handler(){

        @Override

        public void handleMessage(Message msg) {//取消息

            // TODO Auto-generated method stub

            bar.setProgress(msg.arg1);

            updateBarHandler.post(updateThread);

            //super.handleMessage(msg);

        }

        

    };

    

    //线程类,该类使用匿名内部类的方式进行声明

    Runnable updateThread = new Runnable(){

        int i = 0;

        @Override

        public void run() {

            // TODO Auto-generated method stub

            System.out.println("Sign Thread");

            i = i + 10;

            //得到一个消息对象

            Message msg = updateBarHandler.obtainMessage();

            //用arg1和arg2这两个成员变量传递消息,优点是系统性能消耗比较小

            msg.arg1 = i;

            try {

                Thread.sleep(1000);

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            updateBarHandler.sendMessage(msg);//发送消息

            if( i == 100 ){

                updateBarHandler.removeCallbacks(updateThread);

            }

        }

        

    };

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息