博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android ProgressDialog示例
阅读量:2532 次
发布时间:2019-05-11

本文共 6768 字,大约阅读时间需要 22 分钟。

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 .

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 parts

    ProgressBar是一个视图(如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 dismissed

    ProgressDialog是带有“内置” 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 box

    setTitle() :此方法用于为对话框设置标题
  3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) : This method is used to show the horizontal progress bar in the dialog box

    setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) :此方法用于在对话框中显示水平进度条
  4. setProgressStyle(ProgressDialog.STYLE_SPINNER) : This method is used to show the circle/spinning progress bar in the dialog box

    setProgressStyle(ProgressDialog.STYLE_SPINNER) :此方法用于在对话框中显示圆圈/旋转进度条
  5. setMax() : This method is used to set the maximum value

    setMax() :此方法用于设置最大值
  6. getProgress() : This method is used to get the current progress value in numbers

    getProgress() :此方法用于获取数字中的当前进度值
  7. getMax() : This method returns the maximum value of the progress

    getMax() :此方法返回进度的最大值
  8. show(Context context, CharSequence title, CharSequence message) : This is a static method, used to display progress dialog

    show(Context context,CharSequence title,CharSequence message) :这是一个静态方法,用于显示进度对话框
  9. incrementProgressBy(int diff) : This method increments the progress bar by the difference of value passed as a parameter

    crementProgressBy(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

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;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public 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() {                    @Override                    public 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() {                @Override                public 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项目

翻译自:

转载地址:http://cfqzd.baihongyu.com/

你可能感兴趣的文章
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>
备案的问题
查看>>
asp.net下ajax.ajaxMethod使用方法
查看>>
win10+mongodb安装配置
查看>>
window7修改hosts文件
查看>>
【Leetcode_easy】720. Longest Word in Dictionary
查看>>
地铁时光机第一阶段冲刺一
查看>>
Code Smell那么多,应该先改哪一个?
查看>>
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>
Android中的动画
查看>>
LeetCode 119 Pascal's Triangle II
查看>>
【Noip2015pj】求和
查看>>
深入理解JavaScript——闭包
查看>>