Android 学习心得(6)——ProgressBar(进度条)

没什么技术含量就是对系统进度条的简单应用

贴上代码

  1 package cn.bwl.progressbar;
  2 
  3 import android.support.v7.app.ActionBarActivity;
  4 import android.view.View;
  5 import android.widget.Button;
  6 import android.widget.ProgressBar;
  7 import android.widget.TextView;
  8 import android.annotation.SuppressLint;
  9 import android.os.Bundle;
 10 import android.os.Handler;
 11 import android.os.Message;
 12 
 13 public class MainActivity extends ActionBarActivity {
 14      private TextView text1 ;
 15      private TextView text2 ;
 16      private TextView text3 ;
 17      private ProgressBar bar1 ;
 18      private ProgressBar bar2 ;
 19      private ProgressBar bar3 ;
 20      private Button button ;
 21      private int counter;
 22      //自定义Handler消息
 23      protected static final int STOP_1 = 0x1;  
 24      protected static final int STOP_2 = 0x2;  
 25      protected static final int STOP_3 = 0x3;  
 26      protected static final int THREADING_1 = 0x4;  
 27      protected static final int THREADING_2 = 0x5;  
 28      protected static final int THREADING_3 = 0x6;  
 29      
 30      //定义Handler
 31      @SuppressLint("HandlerLeak") 
 32      private Handler handler =new Handler(){
 33             @Override
 34             //当有消息发送出来的时候就执行Handler的这个方法
 35             public void handleMessage(Message msg){
 36             switch (msg.what) {
 37             case STOP_1:
 38                 text1.setText("进度条执行结束");
 39                 bar1.setVisibility(View.GONE);//设置隐藏进度条
 40                 break;
 41             case STOP_2:
 42                 text2.setText("进度条执行结束");
 43                 bar2.setVisibility(View.GONE);//设置隐藏进度条
 44                 break;
 45 
 46             case STOP_3:
 47                 text3.setText("进度条执行结束");
 48                 bar3.setVisibility(View.GONE);//设置隐藏进度条
 49                 break;
 50 
 51             case THREADING_1:
 52                 bar1.setProgress(counter);
 53                 //将进度条显示在TextView控件中
 54                 text1.setText(getResources().getText(counter, "进度条开始")
 55                         + Integer.toString(counter) + "%\n" + "进度:"
 56                                 + Integer.toString(bar1.getProgress()) + "\n"
 57                                 + "Indeterminate:"
 58                                 + Boolean.toString(bar1.isIndeterminate()));
 59                 break;
 60             case THREADING_2:
 61                 bar2.setProgress(counter);
 62                 //将进度条显示在TextView控件中
 63                 text2.setText(getResources().getText(counter, "进度条开始")
 64                         + Integer.toString(counter) + "%\n" + "进度:"
 65                                 + Integer.toString(bar2.getProgress()) + "\n"
 66                                 + "Indeterminate:"
 67                                 + Boolean.toString(bar2.isIndeterminate()));
 68                 break;
 69             case THREADING_3:
 70                 bar3.setProgress(counter);
 71                 //将进度条显示在TextView控件中
 72                 text3.setText(getResources().getText(counter, "进度条开始")
 73                         + Integer.toString(counter) + "%\n" + "进度:"
 74                                 + Integer.toString(bar3.getProgress()) + "\n"
 75                                 + "Indeterminate:"
 76                                 + Boolean.toString(bar3.isIndeterminate()));
 77                 break;
 78 
 79             default:
 80                 break;
 81             }
 82             super.handleMessage(msg);
 83             }
 84             };
 85     @Override
 86     protected void onCreate(Bundle savedInstanceState) {
 87         super.onCreate(savedInstanceState);
 88         setContentView(R.layout.activity_main);
 89         //获取控件句柄
 90         text1 = (TextView) this.findViewById(R.id.text1);
 91         text2 = (TextView) this.findViewById(R.id.text2);
 92         text3 = (TextView) this.findViewById(R.id.text3);
 93         bar1 = (ProgressBar) this.findViewById(R.id.bar1);
 94         bar2 = (ProgressBar) this.findViewById(R.id.bar2);
 95         bar3 = (ProgressBar) this.findViewById(R.id.bar3);
 96         button = (Button) this.findViewById(R.id.button);
 97         //设置indeterminate模式为false
 98         bar1.setIndeterminate(false);
 99         bar2.setIndeterminate(false);
100         bar3.setIndeterminate(false);
101         //button设置监听器
102         button.setOnClickListener(new View.OnClickListener() {
103             
104             @Override
105             public void onClick(View arg0) {
106                 //传输文本进入textView
107                 text1.setText("水平进度条开始!");
108                 text2.setText("小圆圈进度条开始!");
109                 text3.setText("大圆圈进度条开始!");
110                 //设置显示三个进度条控件
111                 bar1.setVisibility(View.VISIBLE);
112                 bar2.setVisibility(View.VISIBLE);
113                 bar3.setVisibility(View.VISIBLE);
114                 //指定三个进度条最大显示
115                 bar1.setMax(100);
116                 bar2.setMax(100);
117                 bar3.setMax(100);
118                 //设置进度条初始值
119                 bar1.setProgress(0);
120                 bar2.setProgress(0);
121                 bar3.setProgress(0);
122                 /*
123                  * 设置新线程
124                  */
125                 new Thread(new Runnable() {
126                     
127                     @Override
128                     public void run() {
129                         //进度条一共跳动10次 做10次循环
130                         for(int i=0;i<10;i++){
131                             try {
132                                 //记录循环进度
133                                 counter =(i+1)*10;
134                                 //线程睡眠1秒
135                                 Thread.sleep(1000);
136                                 //运行到10秒的时候结束
137                                 if(i==9){
138                                     //新建msg
139                                     Message msg1 = new Message();
140                                     Message msg2 = new Message();
141                                     Message msg3 = new Message();
142                                     //用what传递参数,制定传递到handler过去的参数
143                                     msg1.what = MainActivity.STOP_1;
144                                     msg2.what = MainActivity.STOP_2;
145                                     msg3.what = MainActivity.STOP_3;
146                                     //发送消息
147                                     MainActivity.this.handler.sendMessage(msg1);
148                                     MainActivity.this.handler.sendMessage(msg2);
149                                     MainActivity.this.handler.sendMessage(msg3);
150                                 }else{
151                                     //不结束循环每次执行的进程
152                                     //新建msg
153                                     Message msg1 = new Message();
154                                     Message msg2 = new Message();
155                                     Message msg3 = new Message();
156                                     //用what传递参数,制定传递到handler过去的参数
157                                     msg1.what = MainActivity.THREADING_1;
158                                     msg2.what = MainActivity.THREADING_2;
159                                     msg3.what = MainActivity.THREADING_3;
160                                     //发送消息
161                                     MainActivity.this.handler.sendMessage(msg1);
162                                     MainActivity.this.handler.sendMessage(msg2);
163                                     MainActivity.this.handler.sendMessage(msg3);
164                                 }
165                             } catch (InterruptedException e) {
166                                 e.printStackTrace();
167                             }
168                         }
169                     }
170                 }).start();
171                 
172             }
173         });
174 
175     }
176 
177 
178 }
MainActivity.java

界面布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/container"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="cn.bwl.progressbar.MainActivity"
 8     tools:ignore="MergeRootFrame" >
 9 
10     <TextView
11         android:id="@+id/text1"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content" />
14 
15     <ProgressBar
16         android:id="@+id/bar1"
17         style="?android:attr/progressBarStyleHorizontal"
18         android:layout_width="fill_parent"
19         android:layout_height="wrap_content" />
20 
21     <TextView
22         android:id="@+id/text2"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content" />
25 
26     <ProgressBar
27         android:id="@+id/bar2"
28         style="?android:attr/progressBarStyleSmall"
29         android:layout_width="fill_parent"
30         android:layout_height="wrap_content" />
31 
32     <TextView
33         android:id="@+id/text3"
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content" />
36 
37     <ProgressBar
38         android:id="@+id/bar3"
39         style="?android:attr/progressBarStyleLarge"
40         android:layout_width="fill_parent"
41         android:layout_height="wrap_content" />
42 
43     <Button
44         android:id="@+id/button"
45         android:layout_width="wrap_content"
46         android:layout_height="wrap_content"
47         android:text="@string/ok" />
48 
49 </LinearLayout>
activity_main.xml

做这个的时候竟然出奇的没有碰到任何问题,简直是奇迹

posted @ 2015-04-14 21:39  飘0  阅读(277)  评论(0编辑  收藏  举报