zoukankan      html  css  js  c++  java
  • 10.Android之ProgressDialog进度对话框学习

    APP应用中经常会下载某些东西,这里面有涉及到进度对话框,今天来学习下。

    首先,布局里放进两个按钮,点击一个显示条形进度条,另一个显示圆形进度条。代码如下:

     1 <?xml version="1.0" encoding="utf-8"?>  
     2 <LinearLayout   
     3     android:id="@+id/LinearLayout01"      
     4     android:layout_width="match_parent"   
     5     android:layout_height="match_parent"      
     6     android:orientation="vertical"      
     7     xmlns:android="http://schemas.android.com/apk/res/android">
     8 
     9     <Button
    10         android:id="@+id/progress"
    11         android:layout_width="128dp"
    12         android:layout_height="wrap_content"
    13         android:text="条形进度条" />
    14 
    15     <Button
    16         android:id="@+id/circle"
    17         android:layout_width="128dp"
    18         android:layout_height="wrap_content"
    19         android:text="圆形进度条" />
    20 
    21 </LinearLayout>

    显示效果:

    修改MainActivity.java文件:

      1 package com.example.progressdialog;
      2 
      3 import android.app.Activity;
      4 import android.app.ProgressDialog;
      5 import android.content.DialogInterface;
      6 import android.os.Bundle;
      7 import android.view.View;
      8 import android.widget.Button;
      9 
     10 public class MainActivity extends Activity {
     11 
     12     private Button m_btnProgress = null;
     13     private Button m_btnCircle = null;
     14     private ProgressDialog pDialog = null;
     15     private int iCount = 0;
     16 
     17     @Override
     18     protected void onCreate(Bundle savedInstanceState) {
     19         super.onCreate(savedInstanceState);
     20         setContentView(R.layout.activity_main);
     21 
     22         m_btnProgress = (Button) findViewById(R.id.progress);
     23         m_btnCircle = (Button) findViewById(R.id.circle);
     24 
     25         m_btnProgress.setOnClickListener(new View.OnClickListener() {
     26 
     27             @Override
     28             public void onClick(View v) {
     29                 iCount = 0;
     30                 pDialog = new ProgressDialog(MainActivity.this);
     31 
     32                 // 设置进度条风格,风格为长形
     33                 pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     34 
     35                 // 设置ProgressDialog 标题
     36                 pDialog.setTitle("条形进度条");
     37 
     38                 // 设置ProgressDialog 提示信息
     39                 pDialog.setMessage("正在下载中……");
     40 
     41                 // 设置ProgressDialog 标题图标
     42                 pDialog.setIcon(R.drawable.ic_launcher);
     43 
     44                 // 设置ProgressDialog 进度条进度
     45                 pDialog.setProgress(100);
     46 
     47                 // 设置ProgressDialog 的进度条是否不明确
     48                 pDialog.setIndeterminate(false);
     49 
     50                 // 设置ProgressDialog 是否可以按退回按键取消
     51                 pDialog.setCancelable(true);
     52 
     53                 // 让ProgressDialog显示
     54                 pDialog.show();
     55 
     56                 new Thread() {
     57                     public void run() {
     58                         try {
     59                             while (iCount <= 100) {
     60                                 // 由线程来控制进度。
     61                                 pDialog.setProgress(iCount++);
     62                                 Thread.sleep(80);
     63                             }
     64                             pDialog.cancel();
     65                         } catch (InterruptedException e) {
     66 
     67                         }
     68                     }
     69                 }.start();
     70 
     71             }
     72         });
     73 
     74         m_btnCircle.setOnClickListener(new View.OnClickListener() {
     75 
     76             @Override
     77             public void onClick(View v) {
     78 
     79                 iCount = 0;
     80                 // 创建ProgressDialog对象
     81                 pDialog = new ProgressDialog(MainActivity.this);
     82 
     83                 // 设置进度条风格,风格为圆形,旋转的
     84                 pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     85 
     86                 // 设置ProgressDialog 标题
     87                 pDialog.setTitle("圆形进度条");
     88 
     89                 // 设置ProgressDialog 提示信息
     90                 pDialog.setMessage("正在下载中……");
     91 
     92                 // 设置ProgressDialog 标题图标
     93                 pDialog.setIcon(R.drawable.ic_launcher);
     94 
     95                 // 设置ProgressDialog 进度条进度
     96                 pDialog.setProgress(100);
     97 
     98                 // 设置ProgressDialog 的进度条是否不明确
     99                 pDialog.setIndeterminate(false);
    100 
    101                 // 设置ProgressDialog 是否可以按退回按键取消
    102                 pDialog.setCancelable(true);
    103 
    104                 // 设置ProgressDialog 的一个Button
    105                 pDialog.setButton("取消", new DialogInterface.OnClickListener() {
    106                     public void onClick(DialogInterface dialog, int i) {
    107                         // 点击“取消”按钮取消对话框
    108                         dialog.cancel();
    109                     }
    110                 });
    111 
    112                 // 让ProgressDialog显示
    113                 pDialog.show();
    114 
    115                 // 创建线程实例
    116                 new Thread() {
    117                     public void run() {
    118                         try {
    119                             while (iCount <= 100) {
    120                                 // 由线程来控制进度。
    121                                 pDialog.setProgress(iCount++);
    122                                 Thread.sleep(80);
    123                             }
    124                             pDialog.cancel();
    125                         } catch (InterruptedException e) {
    126                             pDialog.cancel();
    127                         }
    128                     }
    129 
    130                 }.start();
    131 
    132             }
    133         });
    134     }
    135 
    136 }

    点击按钮效果:

        

  • 相关阅读:
    yield* 表达式
    Set 对象和WeakSet对象
    洗牌算法
    filter() 方法创建一个新数组
    UTF8文件带BOM引起的问题
    ios的白屏坑
    css的字体样式怎么写
    npm全局安装失效修复
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)解决方案
    linux下nginx的安装及配置
  • 原文地址:https://www.cnblogs.com/benchao/p/5077327.html
Copyright © 2011-2022 走看看