1 import android.app.Activity;
 2 import android.app.ProgressDialog;
 3 import android.os.Bundle;
 4 import android.view.Menu;
 5 import android.view.View;
 6 import android.widget.Button;
 7 
 8 public class MainActivity extends Activity {
 9     ProgressDialog dialog=null;
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         Button btn=(Button) findViewById(R.id.btn);
16         btn.setOnClickListener(new View.OnClickListener() {
17         @Override
18             public void onClick(View arg0) {
19                 dialog=new ProgressDialog(MainActivity.this);
20                 dialog.setMax(100);//设置最大值
21                 dialog.setTitle("安装进度");
22                 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条样式,水平样式
23                 dialog.show();//显示对话框
24                 
25                 /* 
26                  * 定义一个线程,重写run()方法
27                  */
28                 Thread thread=new Thread(){
29                     public void run(){
30                         while(true){
31                             dialog.incrementProgressBy(1);//设置每次增加的进度
32                             /**进度条的值超过100时,让进度条消失*/
33                             if(dialog.getProgress()>=100){
34                                 dialog.dismiss();                            
35                             }
36                             try {
37                                 Thread.sleep(1000);
38                             } catch (InterruptedException e) {
39                                 e.printStackTrace();
40                             }
41                         }
42                     }
43                 };
44                 thread.start();//启动线程
45             }
46         });
47         
48     }
49     
50     
51 
52     @Override
53     public boolean onCreateOptionsMenu(Menu menu) {
54         // Inflate the menu; this adds items to the action bar if it is present.
55         getMenuInflater().inflate(R.menu.main, menu);
56         return true;
57     }
58 
59 }

版权声明:本文为dj168原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dj168/p/3370897.html