android Progressbar使用

yuemeng0 2011-09-09

main.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">  
     
     <TextView 
         android:layout_width="fill_parent"  
         android:layout_height="wrap_content" 
         android:text="Welcome to Mr Wei's blog" />  
     <ProgressBar 
         android:id="@+id/rectangleProgressBar"  
         style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"   
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"
         android:visibility="gone"
         />
     <ProgressBar 
         android:id="@+id/circleProgressBar"  
         style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"
         android:visibility="gone" 
         />
      
     <Button android:id="@+id/button" 
             android:text="Show ProgressBar"  
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content"
             />
 </LinearLayout>
public class ProgressBarDemo extends Activity {

	private ProgressBar rectangleProgressBar, circleProgressBar;
	private Button mButton;

	protected static final int STOP = 0x10000;
	protected static final int NEXT = 0x10001;
	private int iCount = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.progress_bar_demo);
		// findView By id
		rectangleProgressBar = (ProgressBar) findViewById(R.id.rectangleProgressBar);
		circleProgressBar = (ProgressBar) findViewById(R.id.circleProgressBar);
		mButton = (Button) findViewById(R.id.button);

		rectangleProgressBar.setIndeterminate(false);
		circleProgressBar.setIndeterminate(true);

		mButton.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {

				rectangleProgressBar.setVisibility(View.VISIBLE);
				circleProgressBar.setVisibility(View.VISIBLE);

				rectangleProgressBar.setMax(100);
				rectangleProgressBar.setProgress(0);
				circleProgressBar.setProgress(0);

				new IncreaseThread().start();
			}
		});
	}

	// 定义一个Handler
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOP:
				rectangleProgressBar.setVisibility(View.GONE);
				circleProgressBar.setVisibility(View.GONE);
				Thread.currentThread().interrupt();
				break;
			case NEXT:
				if (!Thread.currentThread().isInterrupted()) {
					rectangleProgressBar.setProgress(iCount);
					circleProgressBar.setProgress(iCount);
				}
				break;
			}
		}
	};

	// 每秒步长为5增加,到100%时停止
	class IncreaseThread extends Thread {

		@Override
		public void run() {
			for (int i = 0; i < 20; i++) {
				try {
					iCount = (i + 1) * 5;
					Thread.sleep(1000);
					if (i == 19) {
						Message msg = new Message();
						msg.what = STOP;
						mHandler.sendMessage(msg);
						break;
					} else {
						Message msg = new Message();
						msg.what = NEXT;
						mHandler.sendMessage(msg);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}
}

相关推荐

csuhanshuai / 0评论 2013-05-22
xiaozhifree / 0评论 2013-02-01