📅  最后修改于: 2021-01-05 05:27:01             🧑  作者: Mango
进行进度圈的最简单方法是使用名为ProgressDialog的类。加载栏也可以通过该类制作。条形和圆形之间唯一的逻辑区别是,当您知道等待特定任务的总时间时使用前者,而当您不知道等待时间时使用后者。
为此,您需要实例化此类的对象。它的语法是。
ProgressDialog progress = new ProgressDialog(this);
现在,您可以设置此对话框的某些属性。例如,其样式,其文字等
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
除了这些方法外,ProgressDialog类还提供其他方法。
Sr.No | Classes & Description |
---|---|
1 |
getMax() This methods returns the maximum value of the progress |
2 |
incrementProgressBy(int diff) This method increment the progress bar by the difference of value passed as a parameter |
3 |
setIndeterminate(boolean indeterminate) This method set the progress indicator as determinate or indeterminate |
4 |
setMax(int max) This method set 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 spinning 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.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
private ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();
private long fileSize = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = downloadFile();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarbHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}
});
}
public int downloadFile() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
}else if (fileSize == 200000) {
return 20;
}else if (fileSize == 300000) {
return 30;
}else if (fileSize == 400000) {
return 40;
}else if (fileSize == 500000) {
return 50;
}else if (fileSize == 700000) {
return 70;
}else if (fileSize == 800000) {
return 80;
}
}
return 100;
}
}
将res / layout / activity_main.xml的内容修改为以下内容
在下面的代码中, abc指示tutorialspoint.com的徽标
将res / values /字符串.xml修改为以下内容
My Application
这是默认的AndroidManifest.xml
让我们尝试运行您的应用程序。要从android studio运行该应用,请打开您项目的活动文件之一,然后点击运行工具栏中的图标。在启动应用程序之前,Android Studio将显示以下窗口,以选择要在其中运行Android应用程序的选项。
只需按下按钮即可启动“进度对话框”。按之后,将出现以下屏幕。