Android 中的进度条示例 ProgressDialog

移动开发 Android
进度条用于显示任务的进度。例如。当你从互联网上上传或下载的东西,这更好地显示下载进度/上传给用户。在Android中有一类叫做ProgressDialog,允许创建进度条。为了做到这一点,需要实例化这个类的一个对象。

进度条用于显示任务的进度。例如。当你从互联网上上传或下载的东西,这更好地显示下载进度/上传给用户。

在Android中有一类叫做ProgressDialog,允许创建进度条。为了做到这一点,需要实例化这个类的一个对象。其语法如下:

  1. ProgressDialog progress = new ProgressDialog(this); 

现在,可以设置此对话框的某些属性。比如,它的风格,文本等

  1. progress.setMessage("Downloading Music :) "); 
  2. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
  3. progress.setIndeterminate(true); 

除了这些方法,ProgressDialog类中还提供的其它方法:

Sr. NO 标题与描述
1 getMax()
此方法返回进度的***值
2 incrementProgressBy(int diff)
此方法增加了进度条由值作为参数传递的区别
3 setIndeterminate(boolean indeterminate)
此方法设置进度指示确定或不确定
4 setMax(int max)
此方法设置进度对话框的***值
5 setProgress(int value)
此方法用于更新对话框进度某些特定的值
6 show(Context context, CharSequence title, CharSequence message)
这是一个静态方法,用来显示进度对话框

示例

这个例子说明使用对话框水平进度,事实上这是一个进度条。它在按下按钮时显示进度条。

为了测试这个例子,需要按照以下步骤开发应用程序后,在实际设备上运行。

Steps 描述
1 使用Android Studio创建Android应用程序,并将其命名为ProgressDialogDemo。在创建这个项目时,确保目标SDK和编译在Android SDK***版本和使用更高级别的API
2 修改src/MainActivity.java文件中添加进度代码显示对话框进度
3 修改res/layout/activity_main.xml文件中添加相应的XML代码
4 修改res/values/string.xml文件,添加一个消息作为字符串常量
5 运行应用程序并选择运行Android设备,并在其上安装的应用并验证结果。

以下是修改后的主活动文件的内容 src/com.yiibai.progressdialog/MainActivity.java.

  1. package com.example.progressdialog; 
  2.  
  3. import com.example.progressdialog.R; 
  4.  
  5. import android.os.Bundle; 
  6. import android.app.Activity; 
  7. import android.app.ProgressDialog; 
  8. import android.view.Menu; 
  9. import android.view.View; 
  10.  
  11. public class MainActivity extends Activity { 
  12.  
  13.    private ProgressDialog progress; 
  14.    @Override 
  15.    protected void onCreate(Bundle savedInstanceState) { 
  16.       super.onCreate(savedInstanceState); 
  17.       setContentView(R.layout.activity_main); 
  18.       progress = new ProgressDialog(this); 
  19.    } 
  20.  
  21.  
  22.    public void open(View view){ 
  23.       progress.setMessage("Downloading Music :) "); 
  24.       progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
  25.       //progress.setIndeterminate(true); 
  26.       progress.show(); 
  27.  
  28.    final int totalProgressTime = 100
  29.  
  30.    final Thread t = new Thread(){ 
  31.  
  32.    @Override 
  33.    public void run(){ 
  34.   
  35.       int jumpTime = 0
  36.       while(jumpTime < totalProgressTime){ 
  37.          try { 
  38.             sleep(200); 
  39.             jumpTime += 5
  40.             progress.setProgress(jumpTime); 
  41.          } catch (InterruptedException e) { 
  42.            // TODO Auto-generated catch block 
  43.            e.printStackTrace(); 
  44.          } 
  45.  
  46.       } 
  47.  
  48.    } 
  49.    }; 
  50.    t.start(); 
  51.  
  52.    } 
  53.    @Override 
  54.    public boolean onCreateOptionsMenu(Menu menu) { 
  55.       // Inflate the menu; this adds items to the action bar if it is present. 
  56.       getMenuInflater().inflate(R.menu.main, menu); 
  57.       return true
  58.    } 

修改 res/layout/activity_main.xml 的内容如下

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.    xmlns:tools="http://schemas.android.com/tools" 
  3.    android:layout_width="match_parent" 
  4.    android:layout_height="match_parent" 
  5.    android:paddingBottom="@dimen/activity_vertical_margin" 
  6.    android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.    android:paddingRight="@dimen/activity_horizontal_margin" 
  8.    android:paddingTop="@dimen/activity_vertical_margin" 
  9.    tools:context=".MainActivity" > 
  10.  
  11.    <Button 
  12.       android:id="@+id/button1" 
  13.       android:layout_width="wrap_content" 
  14.       android:layout_height="wrap_content"  
  15.       android:layout_alignParentTop="true" 
  16.       android:layout_centerHorizontal="true" 
  17.       android:layout_marginTop="150dp" 
  18.       android:onClick="open" 
  19.       android:text="@string/download_button" /> 
  20.  
  21.    <TextView 
  22.       android:id="@+id/textView1" 
  23.       android:layout_width="wrap_content" 
  24.       android:layout_height="wrap_content" 
  25.       android:layout_alignParentRight="true" 
  26.       android:layout_alignParentTop="true" 
  27.       android:layout_marginTop="19dp" 
  28.       android:text="@string/download_text" 
  29.       android:textAppearance="?android:attr/textAppearanceLarge" /> 
  30.  
  31. </RelativeLayout> 

修改 res/values/string.xml 以下内容

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.    <string name="app_name">ProgressDialog</string> 
  4.    <string name="action_settings">Settings</string> 
  5.    <string name="hello_world">Hello world!</string> 
  6.    <string name="download_button">Download</string> 
  7.    <string name="download_text">Press the button to download music</string> 
  8. </resources> 

这是默认的 AndroidManifest.xml 文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.    package="com.yiibai.progressdialog" 
  4.    android:versionCode="1" 
  5.    android:versionName="1.0" > 
  6.  
  7.    <uses-sdk 
  8.       android:minSdkVersion="8" 
  9.       android:targetSdkVersion="17" /> 
  10.  
  11.    <application 
  12.       android:allowBackup="true" 
  13.       android:icon="@drawable/ic_launcher" 
  14.       android:label="@string/app_name" 
  15.       android:theme="@style/AppTheme" > 
  16.       <activity 
  17.          android:name="com.yiibai.progressdialog.MainActivity" 
  18.          android:label="@string/app_name" > 
  19.          <intent-filter> 
  20.             <action android:name="android.intent.action.MAIN" /> 
  21.  
  22.             <category android:name="android.intent.category.LAUNCHER" /> 
  23.          </intent-filter> 
  24.       </activity> 
  25.    </application> 
  26.  
  27. </manifest> 

让我们试着运行ProgressDialogDemo应用程序。假设你已经连接实际的Android移动设备到计算机。启动应用程序之前,会显示如下窗口,选择要运行的 Android应用程序的选项。

Anroid

选择移动设备作为一个选项,然后查看移动设备显示如下界面:

Android Progress Dialog Tutorial

只需按下按钮,启动进度条。按下后,如下面的屏幕显示:

Android ProgressDialog

它会不断地自我更新,几秒钟后,出现如下图:

And	roid ProgressDialog


示例代码下载:http://pan.baidu.com/s/1qW9IElQ
责任编辑:闫佳明 来源: yiibai
相关推荐

2015-01-12 12:13:03

Android进度条ProgressDia

2015-07-31 11:19:43

数字进度条源码

2012-07-31 09:53:33

HTML5进度条

2010-01-25 18:27:54

Android进度条

2011-02-22 14:53:41

titlebar标题栏Android

2011-07-05 15:16:00

QT 进度条

2009-06-06 18:54:02

JSP编程进度条

2023-12-11 17:15:05

应用开发波纹进度条ArkUI

2015-08-03 11:39:20

拟物化进度条

2013-03-12 10:35:06

CSS 3

2012-01-17 13:58:17

JavaSwing

2017-03-14 15:09:18

AndroidView圆形进度条

2022-03-07 15:40:51

Linux软件代码

2009-12-25 17:58:12

WPF进度条

2009-08-18 09:49:00

C# listview

2009-08-17 15:48:47

C# WinForm进

2009-08-17 14:41:47

C#进度条实现

2022-04-04 21:33:48

进度条Python

2009-11-24 15:23:50

PHP文件上传进度条

2012-07-13 13:52:54

Canvas
点赞
收藏

51CTO技术栈公众号