如何在 Android 中显示带有百分比的 ProgressBar 示例 [英] How to show a ProgressBar example with percentage in Android
问题描述
我有一项任务是添加一个带有百分比值的 ProgressBar
以在用户想要预测某事时显示一个值.我没有使用 ProgressDialog
因为它现在已被弃用.在这里,百分比值取决于从请求开始到完成所用的时间.我正在使用 Volley
从服务器获取数据.
I have a task to add a ProgressBar
with percentage values on it to show a value when the user wants to predict something. I'm not using ProgressDialog
since it's now deprecated. Here, the percentage value depends on how long it is from start a request until it's completed. I'm using Volley
to fetch data from server.
这是我要实现的目标的示例图像:
Here goes a sample image of what I'm trying to achieve:
我已经这样做了
我在警报对话框中实现了一个水平进度条(我使用这种方式是因为进度对话框已被弃用).我想像这样设置进度条.
i have implement a horizontal progress bar inside an alert dialog (i'm using this way because progress dialog is deprecated). i want to setProgress the progress bar like this.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_result_prediction, container, false);
predictProgress = (ProgressBar) view.findViewById(R.id.progressbarPredict);
AlertDialog.Builder(view.getContext());
builder.setCancelable(false); // if you want user to wait for some process to finish,
View v = inflater.inflate(R.layout.layout_loading_dialog, null);
builder.setView(v);
final AlertDialog dialog = builder.create();
submitPredictPlant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
predictProgress.setProgress(25);
predictProgress.setProgress(50);
predictProgress.setProgress(75);
predictProgress.setProgress(100);
dialog.dismiss();
我想在进度为 100 时关闭对话框.但这会产生类似
i want to dismiss the dialog when the progress is 100. but this give an error like
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setProgress(int)' on a null object reference
我已经从 builder.setview 中使用的 layout_loading_dialog 中引用了进度条.
i already refer the progress bar from layout_loading_dialog that is used in builder.setview.
这里是 layout_loading_dialog.xml
here is the layout_loading_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
<ProgressBar
android:layout_width="match_parent"
android:id="@+id/progressbarPredict"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/progressBarStyleHorizontal"/>
推荐答案
以下是创建如图所示的百分比进度对话框的代码.
Following is the code to create a progress dialog with percentage like in the picture.
int status = 0;
Handler handler = new Handler();
public void showDialog(Activity activity, String msg) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
final ProgressBar text = (ProgressBar) dialog.findViewById(R.id.progress_horizontal);
final TextView text2 = dialog.findViewById(R.id.value123);
new Thread(new Runnable() {
@Override
public void run() {
while (status < 100) {
status += 1;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
text.setProgress(status);
text2.setText(String.valueOf(status));
if (status == 100) {
dialog.dismiss();
}
}
});
}
}
}).start();
dialog.show();
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
布局文件:对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:paddingLeft="20dp"
android:layout_margin="16dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/value123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/value123"
android:text="%" />
</RelativeLayout>
结果:
使用异步任务:
int status = 0;
Handler handler = new Handler();
Dialog dialog;
ProgressBar text;
TextView text2;
public void showDialog(Activity activity, String msg) {
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
text = (ProgressBar) dialog.findViewById(R.id.progress_horizontal);
text2 = dialog.findViewById(R.id.value123);
dialog.show();
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
new Thread(new Runnable() {
@Override
public void run() {
while (status < 100) {
status += 1;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
text.setProgress(status);
text2.setText(String.valueOf(status));
if (status == 100) {
status = 0;
}
}
});
}
}
}).start();
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// it will fill the progress bar to 100
// and will refill again and again
/*
*
* Make your api call here
* MAke request to API and return data when response comes
*
*
* */
/*
*
* I have just add sleep to thead for example purposes
*
* */
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
/*
*
*
* Do your processing there on the api result
*
*
* */
dialog.dismiss();
}
@Override
protected void onPreExecute() {
showDialog(MainActivity.this,"");
}
@Override
protected void onProgressUpdate(String... text) {
}
}
启动异步任务:
new AsyncTaskRunner().execute("fs");
我不确定这是否是最好的方法
I am not sure if this is the best approach
这篇关于如何在 Android 中显示带有百分比的 ProgressBar 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!