本文共 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 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);
Some important attributes of android ProgressDialog are given below.
android ProgressDialog的一些重要属性如下。
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毫秒增加一次。
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/