Welcome to Android ProgressDialog Example. In this tutorial we’ll learn how to create Android Progress Dialog containing a ProgressBar. Also we’ll discuss at length the difference between a ProgressDialog and ProgressBar.

欢迎使用Android ProgressDialog示例。 在本教程中,我们将学习如何创建包含ProgressBar的Android Progress对话框。 我们还将详细讨论ProgressDialog和ProgressBar之间的区别。

Android ProgressDialog (Android ProgressDialog)

Android ProgressDialog is an extension of AlertDialog. To know more about an AlertDialog, check out it’s tutorial here.

Android ProgressDialog是AlertDialog的扩展。 要了解有关AlertDialog的更多信息,请在此处查看其教程。

Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. Android Progress Dialog is almost same as ProgressBar with the exception that this is displayed as a dialog box.

Android ProgressDialog是一个对话框/对话框窗口,显示任务的进度。 Android进度对话框与ProgressBar几乎相同,除了它显示为对话框。

In order to create a ProgressDialog to display a ProgressBar we need to instantiate it like this.

为了创建一个ProgressDialog来显示一个ProgressBar,我们需要像这样实例化它。

ProgressDialog progress = new ProgressDialog(this);

Android ProgressDialog和ProgressBar之间的区别 (Difference between Android ProgressDialog and ProgressBar)

  1. ProgressBar is a View (like TextView, ImageView, Button, etc..), which can be used in the layout to show some progress. A ProgressBar is used to indicate that something in tge app is still loading while the user may still interact with other partsProgressBar是一个视图(如TextView,ImageView,Button等。),可以在布局中使用它来显示一些进度。 ProgressBar用于指示tge应用程序中的某些内容仍在加载,而用户可能仍与其他部分进行交互
  2. ProgressDialog is a Dialog with ‘built-in’ ProgressBar. A ProgressDialog is used when we want to prevent the user from interacting with the application while waiting. The Dialog aspect freezes the user from doing anything until it is dismissedProgressDialog是带有“内置” ProgressBar的对话框。 当我们要防止用户在等待时与应用程序进行交互时,可以使用ProgressDialog。 对话框方面使用户无法做任何事情,直到被解雇为止

Android ProgressDialog属性 (Android ProgressDialog Attributes)

Some important attributes of android ProgressDialog are given below.

android ProgressDialog的一些重要属性如下。

  1. setMessage() : This method is used to show the message to the user. Example: Loading…setMessage() :此方法用于向用户显示消息。 示例:正在加载…
  2. setTitle() : This method is used to set a title to the dialog boxsetTitle() :此方法用于为对话框设置标题
  3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) : This method is used to show the horizontal progress bar in the dialog boxsetProgressStyle(ProgressDialog.STYLE_HORIZONTAL) :此方法用于在对话框中显示水平进度条
  4. setProgressStyle(ProgressDialog.STYLE_SPINNER) : This method is used to show the circle/spinning progress bar in the dialog boxsetProgressStyle(ProgressDialog.STYLE_SPINNER) :此方法用于在对话框中显示圆圈/旋转进度条
  5. setMax() : This method is used to set the maximum valuesetMax() :此方法用于设置最大值
  6. getProgress() : This method is used to get the current progress value in numbersgetProgress() :此方法用于获取数字中的当前进度值
  7. getMax() : This method returns the maximum value of the progressgetMax() :此方法返回进度的最大值
  8. show(Context context, CharSequence title, CharSequence message) : This is a static method, used to display progress dialogshow(Context context,CharSequence title,CharSequence message) :这是一个静态方法,用于显示进度对话框
  9. incrementProgressBy(int diff) : This method increments the progress bar by the difference of value passed as a parametercrementProgressBy(int diff) :此方法将进度条增加作为参数传递的值的差

In this tutorial we’ll develop an application that displays a ProgressDialog containing a horizontal ProgressBar which increments after every 200 milliseconds.

在本教程中,我们将开发一个显示一个ProgressDialog的应用程序,其中包含一个水平的ProgressBar,每200毫秒增加一次。

Android ProgressDialog项目结构 (Android ProgressDialog Project Structure)

Android ProgressDialog示例 (Android ProgressDialog Example)

The activity_main.xml contains a Button which invokes a ProgressDialog on click as shown in the xml code below:

activity_main.xml包含一个按钮,单击该按钮将在单击时调用ProgressDialog,如下面的xml代码所示:

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" Start ProgressDialog"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="57dp" /></RelativeLayout>

The MainActivity.java file is given below.

MainActivity.java文件在下面给出。

MainActivity.java

MainActivity.java

package com.journaldev.progressdialog;import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {Button button;ProgressDialog progressDoalog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {progressDoalog = new ProgressDialog(MainActivity.this);progressDoalog.setMax(100);progressDoalog.setMessage("Its loading....");progressDoalog.setTitle("ProgressDialog bar example");progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDoalog.show();new Thread(new Runnable() {@Overridepublic void run() {try {while (progressDoalog.getProgress() <= progressDoalog.getMax()) {Thread.sleep(200);handle.sendMessage(handle.obtainMessage());if (progressDoalog.getProgress() == progressDoalog.getMax()) {progressDoalog.dismiss();}}} catch (Exception e) {e.printStackTrace();}}}).start();}Handler handle = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);progressDoalog.incrementProgressBy(1);}};});}
}

The following code activates the handler in which we write the code to increment the progress bar.

以下代码激活了处理程序,在该处理程序中,我们编写了代码以递增进度栏。

handle.sendMessage(handle.obtainMessage());

Below is the output video when you will run the android progress dialog example application in android emulator.

以下是您将在android模拟器中运行android progress对话框示例应用程序时的输出视频。

This brings an end to Android ProgressDialog Example tutorial. You can download the final Android ProgressDialog Project from the below link.

这结束了Android ProgressDialog示例教程。 您可以从下面的链接下载最终的Android ProgressDialog项目

Download Android ProgressDialog Project下载Android ProgressDialog项目

翻译自: https://www.journaldev.com/9652/android-progressdialog-example

Android ProgressDialog示例相关推荐

  1. Android ProgressBar示例

    Welcome to Android ProgressBar Example. Today we'll implement android ProgressBar in our application ...

  2. Android AsyncTask示例教程

    Today we will look into Android AsyncTask. We will develop an Android example application that perfo ...

  3. android jni示例_Android服务示例

    android jni示例 A service is a component that runs in the background for supporting different types of ...

  4. Android 经典示例,初学者的绝好源码资料

    2019独角兽企业重金招聘Python工程师标准>>> Android 经典示例,初学者的绝好源码资料 附上源码: 转载:http://www.adobex.com/android/ ...

  5. android 工程搭建,Android ApiDemo示例工程的创建

    一般SDK中都会带有一些示例程序,说明具体用法,Android SDK也是如此.它提供了一些ApiDemo示例,详细说明了Android中主要API,分为以下几大类: 1.App 2.Content ...

  6. Android ListView示例教程

    We will learn how to create a simple Android ListView and launch a new activity on selecting a singl ...

  7. android jni示例_Android动画示例

    android jni示例 Android Animation is used to give the UI a rich look and feel. Animations in android a ...

  8. Android ActionBar示例教程

    Today we will look into Android ActionBar. Action Bar is one of the important part of any applicatio ...

  9. Android WebView示例教程

    Android WebView is used to display HTML in an android app. We can use android WebView to load HTML p ...

最新文章

  1. Ether-channel 以太网通道
  2. 为什么以太网帧的长度最短64字节,最长1518字节?
  3. 分享是程序员的必备素质
  4. c语言delay_C语言编程制作“古怪手电筒”,有光的时候就会亮,没光绝不会亮...
  5. Tensorflow学习: 乘法demo
  6. python中loop函数运用_使用涉及函数的Python在for循环中填充DataFrame
  7. 视觉中的经典图像特征小结(一): 颜色直方图, HOG, LBP
  8. java栅格法全局路径规划,基于A*的全局路径规划算法(1)
  9. 加密扩展库 php,如何利Mcrypt扩展库进行加密和解密_php
  10. SqlServer动态表查询
  11. 关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;...
  12. san分布式共享文件系统_基于SAN存储共享卷实现openstack高可用的方法与流程
  13. 分享超级表格用户在知乎上与我们的对话
  14. 银行转账java mysql_一个银行转账业务模型分析:大魏Java记5-7
  15. zepto 操作 cookie
  16. 【转载】 MySQL数据库“十宗罪”(十大经典错误案例)
  17. 网络诊断 网络连接配置
  18. job每分钟执行 oracle_oracle的job怎么设置一个过程每5分钟执行一次
  19. C++ 语法篇之 static 用法
  20. 关于请求报文和响应报文的详解

热门文章

  1. Oracle学习笔记之五sp1,PL/SQL之BULK COLLECT
  2. Git命令行介绍和使用说明(持续更新)
  3. ASP.NET程序中常用代码汇总-1
  4. [转载] 令牌桶算法和漏桶算法python_排序算法(七):Bucket Sort 桶排序
  5. [转载] python radians函数_Python numpy.radians() 使用实例
  6. Echarts Y轴min显示奇葩问题(做此记录)
  7. 移动端rem布局(阿里)
  8. Java学习之道:Java中十个常见的违规编码
  9. ADT版本不同导致的一个问题
  10. Separate texture from black background