📅  最后修改于: 2021-01-05 05:27:32             🧑  作者: Mango
进度条用于显示任务的进度。例如,当您从Internet上传或下载某些内容时,最好向用户显示下载/上传的进度。
在android中,有一个名为ProgressDialog的类,可让您创建进度条。为此,您需要实例化此类的对象。它的语法是。
ProgressDialog progress = new ProgressDialog(this);
现在,您可以设置此对话框的某些属性。例如,其样式,其文本等。
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
除了这些方法外,ProgressDialog类还提供其他方法
Sr. No | Title & description |
---|---|
1 |
getMax() This method returns the maximum value of the progress. |
2 |
incrementProgressBy(int diff) This method increments the progress bar by the difference of value passed as a parameter. |
3 |
setIndeterminate(boolean indeterminate) This method sets the progress indicator as determinate or indeterminate. |
4 |
setMax(int max) This method sets the maximum value of the progress dialog. |
5 |
setProgress(int value) This method is used to update the progress dialog with some specific value. |
6 |
show(Context context, CharSequence title, CharSequence message) This is a static method, used to display progress dialog. |
此示例演示了进度对话框的水平用法,实际上它是进度条。按下按钮时显示进度条。
要尝试使用此示例,您需要根据以下步骤在开发应用程序之后在实际设备上运行此示例。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add progress code to display the progress dialog. |
3 | Modify res/layout/activity_main.xml file to add respective XML code. |
4 | Run the application and choose a running android device and install the application on it and verify the results. |
以下是修改后的主要活动文件src / MainActivity.java的内容。
package com.example.sairamkrishna.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button2);
}
public void download(View view){
progress=new ProgressDialog(this);
progress.setMessage("Downloading Music");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
将res / layout / activity_main.xml的内容修改为以下内容:
这是默认的AndroidManifest.xml-
让我们尝试运行您的应用程序。我们假设您已将实际的Android Mobile设备与计算机连接。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击运行工具栏中的图标。在启动应用程序之前,Android Studio将显示以下窗口,以选择要在其中运行Android应用程序的选项。
选择您的移动设备作为选项,然后检查将显示以下屏幕的移动设备-
只需按下按钮即可启动进度栏。按下后,将出现以下屏幕-
它将不断更新自身。