Android ProgressDialog示例

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

<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" >

    <Button
        android: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;

    @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项目

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值