[size=large][color=brown][b]布局文件:[/b][/color][/size]
[size=medium][color=darkblue][b]主程序入口:[/b][/color][/size]
[size=large][color=indigo][b]
和 Handler的用法:[/b][/color][/size]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 定义 ProgresBar组件
style="?android:attr/progressBarStyleHorizontal" :设置进度条为水平进度条
-->
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_margin="10px"
/>
<Button
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
[size=medium][color=darkblue][b]主程序入口:[/b][/color][/size]
package com.example.progressbar;
import java.util.Timer;
import java.util.TimerTask;
import org.xml.sax.HandlerBase;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tx;
private int count = 0;
// 自定义 Handler信息代码,用以作为识别事件处理
protected static final int Start_NOTIFIER=0x101;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx = (TextView) findViewById(R.id.title);
uppUI();
}
/**
* 实现 6 秒钟更新 UI 界面 TX 的显示
*/
public void uppUI() {
Timer timer = new Timer(); // 创建爱你Timer 时钟对象
timer.scheduleAtFixedRate(new MyShedule(), 1, 1000); //每隔 6 秒中执行 MyShedule
}
/**
* 实现更新的动作
* @author Administrator
*
*/
private class MyShedule extends TimerTask {
public void run() {
Message message = new Message(); //创建message对象
message.what = MainActivity.Start_NOTIFIER; //用于自定义消息代码,以便收件人收到信息
handler.sendMessage(message); //向Handler发送消息
}
}
//创建Handler 对象,通过实现handleMessage方法,接收信息
Handler handler = new Handler(){
//注意:子类必须实现这个方法才能接收信息
public void handleMessage(Message msg) {
switch (msg.what) { //判断消息代码值
case MainActivity.Start_NOTIFIER:
tx.setText(new Integer(count).toString());
count++;
break;
}
}
};
}
[size=large][color=indigo][b]
和 Handler的用法:[/b][/color][/size]
package com.example.progressbar;
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;
import android.widget.TextView;
public class ProgressBarAndHandler extends Activity {
private TextView tx;
private ProgressBar progressbar;
private Button download;
private int count = 0;
// 自定义 Handler信息代码,用以作为识别事件处理
protected static final int STOP_Flag = 0x100;
protected static final int THREADING_Flag = 0x101;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取各种对象
tx = (TextView) findViewById(R.id.title);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
download = (Button) findViewById(R.id.download);
// 设置按钮单机事件
download.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
actionForDownload();
}
});
}
/**
* 按钮点击动作
*/
public void actionForDownload() {
tx.setText(R.string.start); // 设置按钮上显示值
progressbar.setVisibility(View.VISIBLE); // 设置progressbar 为可见状态
progressbar.setMax(100); // 设置最大值为:100
progressbar.setProgress(0); // 设置当前值为0
progressbar.setIndeterminate(false); // 设置进度条为明确显示
new Thread() {
public void run() {
while (count <= 100) {
// 小于 100 的时候修改当前进度值
try {
count = count + 1;
Thread.sleep(100); // 休眠 0.1秒
// 如果值累加到了 100 则,向Handler发送 STOP_Flag 消息
if (count == 100) {
Message message = new Message();
message.what = ProgressBarAndHandler.STOP_Flag;
// 向Handler发送信息代码
ProgressBarAndHandler.this.handler
.sendMessage(message);
break;
} else { // 不等于 100 发送THREADING_Flag 消息
Message m = new Message();
m.what = ProgressBarAndHandler.THREADING_Flag;
ProgressBarAndHandler.this.handler.sendMessage(m);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}.start();
}
// 根据消息进行对应的处理
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case ProgressBarAndHandler.STOP_Flag: // 表示下载完毕
tx.setText("下载完毕");
download.setText("下载完毕");
progressbar.setVisibility(View.GONE); //设置进度条不可见
Thread.currentThread().interrupt(); //中断当前线程
break;
case ProgressBarAndHandler.THREADING_Flag: // 表示正在下载中
progressbar.setProgress(count); //修改当前进度的值
tx.setText(getResources().getText(R.string.start)+Integer.toString(count)+"%)\n"+"Progress:"+Integer.toString(progressbar.getProgress()));
break;
}
super.handleMessage(msg);
}
};
}