• ProgressDialog进度条对话框


    (一)

    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 }
  • 相关阅读:
    “指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单
    windows下如何快速优雅的使用python的科学计算库?
    量化分析师的Python日记【第1天:谁来给我讲讲Python?】
    Python的lambda函数与排序
    使用python管理Cisco设备-乾颐堂
    python移除系统多余大文件-乾颐堂
    python算法
    python实现高效率的排列组合算法-乾颐堂
    使用python把图片存入数据库-乾颐堂
    Python将阿拉伯数字转化为中文大写-乾颐堂
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9070915.html
Copyright © 2020-2023  润新知