Android控件之-ProgressBar_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android控件之-ProgressBar

Android控件之-ProgressBar

 2010/12/15 8:00:50  ko8e  http://ko8e.javaeye.com  我要评论(0)
  • 摘要:Android手机中进度条的使用主要是两种,分别是长形进度条和圆形进度条进度条的值默认是100packagecom.ko8e;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.view.View;importandroid.widget.Button;importandroid.widget.ProgressBar
  • 标签:android 控件 ProgressBar Android控件

Android手机中进度条的使用主要是两种,分别是长形进度条和圆形进度条

进度条的值默认是100

package com.ko8e;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MyActivity extends Activity {
	/** Called when the activity is first created. */
	private ProgressBar progressBar1 = null;
	private ProgressBar progressBar2 = null;
	private Button button = null;
	private static final int GUI_STOP_NOTIFIER = 0x108;
	private static final int GUI_THREADING_NOTIFIER = 0x109;
	int intCounter = 0;

	@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);
		button = (Button) findViewById(R.id.button);

		progressBar1.setIndeterminate(false);
		progressBar2.setIndeterminate(false);

		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				// 设置progressBar状态可见
				progressBar1.setVisibility(View.VISIBLE);
				progressBar2.setVisibility(View.VISIBLE);
				// 设置progressBar1的最大值
				progressBar1.setMax(100);
				// 设置progressBar的当前值
				progressBar1.setProgress(0);
				progressBar2.setProgress(0);

				new Thread(new Runnable() {
					public void run() {
						for (int i = 0; i < 10; i++) {
							try {
								intCounter = (i + 1) * 20;
								Thread.sleep(1000);
								if (i == 4) {
									Message msg = new Message();
									msg.what = MyActivity.GUI_STOP_NOTIFIER;
									MyActivity.this.myMessageHandler
											.sendMessage(msg);
									break;
								} else {
									Message msg = new Message();
									msg.what = MyActivity.GUI_THREADING_NOTIFIER;
									MyActivity.this.myMessageHandler
											.sendMessage(msg);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}).start();
			}
		});
	}

	Handler myMessageHandler = new Handler() {

		public void handleMessage(Message m) {
			switch (m.what) {
			case MyActivity.GUI_STOP_NOTIFIER:
				progressBar1.setVisibility(View.GONE);
				progressBar2.setVisibility(View.GONE);
				Thread.currentThread().interrupt();
				break;
			case MyActivity.GUI_THREADING_NOTIFIER:
				if (!Thread.currentThread().isInterrupted()) {
					progressBar1.setProgress(intCounter);
					progressBar2.setProgress(intCounter);
					setProgress(intCounter * 100);
					setSecondaryProgress(intCounter * 100);
				}
				break;
			}
			super.handleMessage(m);
		}
	};
}

?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:id="@+id/textView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 <ProgressBar
 	android:id="@+id/progressBar1"
 	style="?android:attr/progressBarStyleHorizontal"
 	android:layout_width="200dp"
 	android:layout_height="wrap_content"
 	android:visibility="gone"
 	/>   
<ProgressBar
	android:id="@+id/progressBar2"
	style="?android:attr/progressBarStyleLarge"
	android:max="100"
	android:progress="50"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:secondaryProgress="70"
	android:visibility="gone"
        //gone 表示不可视,需要在程序中设置可视
?/>
<Button
	android:id="@+id/button"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="@string/start"	
	/>	
</LinearLayout>
发表评论
用户名: 匿名