消息机制--ProgressBar

来源:互联网 发布:花呗只能在淘宝还款吗 编辑:程序博客网 时间:2024/06/05 00:49









package org.lxh.demo;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 MyProgressBarDemo extends Activity {private ProgressBar myprobarA, myprobarB, myprobarC, myprobarD, myprobarE;    private Button mybut;   // 控制按钮    protected static final int STOP = 1;// 停止消息    protected static final int CONTINUE = 2;// 继续消息@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.myprobarA = (ProgressBar) this.findViewById(R.id.myprobarA);// 取得进度条this.myprobarB = (ProgressBar) this.findViewById(R.id.myprobarB); // 取得进度条this.myprobarC = (ProgressBar) this.findViewById(R.id.myprobarC); // 取得进度条this.myprobarD = (ProgressBar) this.findViewById(R.id.myprobarD); // 取得进度条this.myprobarE = (ProgressBar) this.findViewById(R.id.myprobarE); // 取得进度条this.mybut = (Button) this.findViewById(R.id.mybut); // 取得按钮this.myprobarA.setIndeterminate(false); // 确定不确定模式this.myprobarB.setIndeterminate(false); // 确定不确定模式this.myprobarC.setIndeterminate(true); // 确定确定模式this.myprobarD.setIndeterminate(false); // 确定不确定模式this.myprobarE.setIndeterminate(false); // 确定不确定模式this.mybut.setOnClickListener(new OnClickListenerImpl()) ;// 设置单击事件}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {   // 单击事件MyProgressBarDemo.this.myprobarB.setSecondaryProgress(0); // 第二进度条MyProgressBarDemo.this.myprobarA.setVisibility(View.VISIBLE); // 组件可见MyProgressBarDemo.this.myprobarB.setVisibility(View.VISIBLE); // 组件可见MyProgressBarDemo.this.myprobarC.setVisibility(View.VISIBLE); // 组件可见MyProgressBarDemo.this.myprobarD.setVisibility(View.VISIBLE); // 组件可见MyProgressBarDemo.this.myprobarE.setVisibility(View.VISIBLE); // 组件可见MyProgressBarDemo.this.myprobarA.setMax(120); // 设置最大值MyProgressBarDemo.this.myprobarB.setMax(120); // 设置最大值MyProgressBarDemo.this.myprobarA.setProgress(0); // 设置当前值MyProgressBarDemo.this.myprobarB.setProgress(0); // 设置当前值new Thread(new Runnable() {public void run() { // 线程主体int count = 0; // 用于保存当前进度值for (int i = 0; i < 10; i++) { // 循环设置内容try {count = (i + 1) * 20; // 设置进度条当前值Thread.sleep(500); // 休眠0.5秒if (i == 6) { // 如果为6,则进度为120Message m = new Message(); // 定义消息m.what = MyProgressBarDemo.STOP; // 消息代码MyProgressBarDemo.this.myMessageHandler.sendMessage(m); // 发送消息break; // 循环中断} else {Message m = new Message(); // 定义消息m.arg1 = count; // 设置参数m.what = MyProgressBarDemo.CONTINUE; // 消息代码MyProgressBarDemo.this.myMessageHandler.sendMessage(m); // 发送消息}} catch (Exception ex) { // 处理sleep()异常ex.printStackTrace();}}}}).start(); // 启动线程        }}private Handler myMessageHandler = new Handler() {// 共用一个Handler@Overridepublic void handleMessage(Message msg) {switch (msg.what) {// 判断消息类型case MyProgressBarDemo.STOP:// 停止记录myprobarA.setVisibility(View.GONE);// 组件不可见myprobarB.setVisibility(View.GONE);// 组件不可见myprobarC.setVisibility(View.GONE);// 组件不可见myprobarD.setVisibility(View.GONE);// 组件不可见myprobarE.setVisibility(View.GONE);// 组件不可见Thread.currentThread().interrupt();// 组件不可见break;case MyProgressBarDemo.CONTINUE:// 组件增长if (!Thread.currentThread().isInterrupted()) {myprobarA.setProgress(msg.arg1);// 设置当前进度myprobarB.setProgress(msg.arg1);// 设置当前进度myprobarC.setProgress(msg.arg1);// 设置当前进度myprobarD.setProgress(msg.arg1);// 设置当前进度myprobarE.setProgress(msg.arg1);// 设置当前进度}break;}}};}


<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/MyLayout"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ProgressBarandroid:id="@+id/myprobarA"style="?android:attr/progressBarStyle"android:visibility="gone"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ProgressBarandroid:id="@+id/myprobarB"style="?android:attr/progressBarStyleHorizontal"android:visibility="gone"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ProgressBarandroid:id="@+id/myprobarC"style="?android:attr/progressBarStyleHorizontal"android:visibility="gone"android:max="120"android:progress="0"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ProgressBarandroid:id="@+id/myprobarD"android:visibility="gone"android:max="120"android:progress="50"android:secondaryProgress="70"style="?android:attr/progressBarStyleLarge"android:layout_width="fill_parent"android:layout_height="wrap_content"/><ProgressBarandroid:id="@+id/myprobarE"android:visibility="gone"android:max="120"android:progress="50"android:secondaryProgress="70"style="?android:attr/progressBarStyleSmall"android:layout_width="fill_parent"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/mybut"android:text="显示进度条"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>


0 0
原创粉丝点击