还是使用Android 2.3.3版本。
一、界面
二、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txtInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="2dip" android:gravity="center_horizontal" android:text="进度条演示案例" android:textSize="18sp" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dip" android:visibility="gone" > </ProgressBar> <Button android:id="@+id/startButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dip" android:text="开始" > </Button> </LinearLayout>
三、Activity类
package org.e276.progress;
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;
import android.widget.TextView;
/**
* Handler类 有2个队列,1个线程队列,1个消息队列
*
* @author miao
*
*/
public class ProgressBarDemoActivity extends Activity {
private ProgressBar progressBar;// 进度条
private Button startButton;// 按钮
private TextView txtInfo;// 文本框
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 进度条
progressBar = (ProgressBar) findViewById(R.id.progressBar);
startButton = (Button) findViewById(R.id.startButton);
txtInfo = (TextView) findViewById(R.id.txtInfo);
// 为控件设置监听器
startButton.setOnClickListener(new ProgressBarOnClickListener());
}
// 使用匿名内部类来充血Handler当中的handlerMessage()方法
Handler handler = new Handler() {
@Override
// 处理消息,与sendMessage的执行是异步的
public void handleMessage(Message msg) {
// 设置进度条的刻度
progressBar.setProgress(msg.arg1);
// 更新文本显示
txtInfo.setText("当前进度" + msg.arg1 + "%");
if (msg.arg1 >= 100) {
// 将线程对象从队列中移除
handler.removeCallbacks(updateThread);
txtInfo.setText("加载完成");
// 按钮可用
startButton.setEnabled(true);
} else {
// 处理完当前的消息,再放一个线程到队列中
handler.post(updateThread);
//如果想把进度的速度调慢,就这样:handler.postDelayed(updateThread,200);
}
}
};
// 声明一线程
Runnable updateThread = null;
// 另一个线程,该类使用匿名内部类的方式进行声明
class UpdateThread implements Runnable {
int i = 0;
public void run() {
i++;
// 得到一个消息对象
Message msg = handler.obtainMessage();
// 将Message对象的arg1参数的值设置为i
msg.arg1 = i;// 用arg1、arg2这两个成员变量传递消息,优点是系统性能消耗较少
// 将Message对象加入到消息队里欸当中
handler.sendMessage(msg);
}
}
// 按钮点击事件
class ProgressBarOnClickListener implements OnClickListener {
public void onClick(View v) {
// 设置进度条为可见状态,在main.xml中设置为visibility="gone"不可见
progressBar.setVisibility(View.VISIBLE);
// 实例化一个线程
updateThread = new UpdateThread();
// 将要执行的线程放入到队列当中
handler.post(updateThread);
// 按钮不可用
startButton.setEnabled(false);
}
}
}
四、demo