码迷,mamicode.com
首页 > 其他好文 > 详细

ProgressDialog进度条对话框

时间:2018-05-22 12:59:56      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:size   val   int   bsp   情况   instance   dialog   miss   button   

(一)

1.效果图:

技术分享图片

2.activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.administrator.hello3.MainActivity">
11 
12     <Button
13         android:id="@+id/btn"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="显示进度条" />
17 </RelativeLayout>

3.MainActivity.java

 1 package com.example.administrator.hello3;
 2 
 3 import android.app.Activity;
 4 import android.app.ProgressDialog;
 5 import android.os.Handler;
 6 import android.os.Message;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.Button;
11 
12 public class MainActivity extends Activity {
13 
14     private Button button;
15     private ProgressDialog pd;
16     private final static  int MAXVALUE=100;
17     private int progressStatus =0; //进度条对话框的百分比情况
18     private int data[] = new int[100];//填充数组
19     private  int current = 0;
20     private Handler handler = new Handler(){
21         @Override
22         public void handleMessage(Message msg) {
23             if(msg.what==0x123){
24                 pd.setProgress(progressStatus);
25             }
26 
27             super.handleMessage(msg);
28         }
29     };
30 
31     @Override
32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(R.layout.activity_main);
35 
36         button=(Button)findViewById(R.id.btn);
37         button.setOnClickListener(new View.OnClickListener() {
38             @Override
39             public void onClick(View v) {
40                 progressStatus=0;
41                 current=0;
42                 pd=new ProgressDialog(MainActivity.this);
43                 pd.setMax(MAXVALUE);
44                 pd.setTitle("任务完成百分比");
45                 pd.setMessage("耗时任务完成百分比");
46                 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
47 
48                 pd.show();
49 
50                 new Thread(new Runnable() {
51                     @Override
52                     public void run() {
53                         while (progressStatus<MAXVALUE){
54                             progressStatus=MAXVALUE*dowork()/data.length;
55                             handler.sendEmptyMessage(0x123);
56                         }
57                         if(progressStatus>=MAXVALUE){
58                             pd.dismiss();
59                         }
60                     }
61                 }).start();
62             }
63         });
64     }
65 
66     public  int dowork(){
67         data[current++]=(int)(Math.random()*100);
68         try {
69             Thread.sleep(100);
70         }catch (InterruptedException e){
71             e.printStackTrace();
72         }
73         return current;
74     }
75 }

 

ProgressDialog进度条对话框

标签:size   val   int   bsp   情况   instance   dialog   miss   button   

原文地址:https://www.cnblogs.com/sunxiaoyan/p/9070915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!