Android 如何停止和启动 ProgressBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11314272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop and start a ProgressBar
提问by saqibabbasi
I am unable to stop a ProgressBar
. Its style is ProgressBarStylesmall
. How can I start and stop a circular, small ProgressBar
?
我无法阻止ProgressBar
. 它的风格是ProgressBarStylesmall
。我怎样才能开始和停止一个圆形的小ProgressBar
?
回答by Riten
By default you can make progressBar visible and hide it when not needed like this:
默认情况下,您可以使 progressBar 可见并在不需要时将其隐藏,如下所示:
progressBar.setVisibility(View.GONE);
If you want to control it programatically then you can achieve that with:
如果您想以编程方式控制它,那么您可以通过以下方式实现:
progressBar.setVisibility(View.VISIBLE); //to show
progressBar.setVisibility(View.GONE); // to hide
You can even go with ProgressDialog:
你甚至可以使用 ProgressDialog:
private ProgressDialog progressDialog;
...
public void showProgressDialog() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(PhotographerDetailActivity.this);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
}
progressDialog.setMessage("your message");
progressDialog.show();
}
public void dismissProgressDialog() {
if (progressDialog != null ) {
progressDialog.dismiss();
}
Hope it will help you.
希望它会帮助你。
回答by Charlie-Blake
You'll need to handle AsyncTasks for this.
您需要为此处理 AsyncTasks。
/*
* We set the visibility to true. If it was already visible
* there's no need of doing this but we do it for the sake of readability.
*/
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1);
progressBar.setVisibility(View.VISIBLE);
new AsyncTask<Void,Void,Void>(){
//The variables you need to set go here
@Override
protected Void doInBackground(final Void... params){
// Do your loading here. Don't touch any views from here, and then return null
return null;
}
@Override
protected void onPostExecute(final Void result){
// Update your views here
progressBar.setVisibility(View.GONE);
}
}.execute()
回答by Angelo
I don't know if that helps, but you can use a ProgressDialog
. Let's say you have a running ProgressDialog
named progressDialog
you can use progressDialog.dismiss();
. Just make sure the instance of the ProgressDialog
you are dismissing is the ProgressDialog
that is actually showing. You can check it with progressDialog.isShowing();
. To start a ProgressDialog
:
我不知道这是否有帮助,但您可以使用ProgressDialog
. 假设您有一个正在运行的ProgressDialog
名称,progressDialog
您可以使用progressDialog.dismiss();
. 只要确保ProgressDialog
您正在解雇的实例ProgressDialog
是实际显示的实例。您可以使用progressDialog.isShowing();
. 开始ProgressDialog
:
progressDialog = ProgressDialog
.show(myActivityContext,
"ProgressDialog Title",
"ProgressDialog Body");
By default the ProgressDialog
UI is circular just like you need it.
默认情况下,ProgressDialog
UI 是圆形的,就像您需要的那样。
回答by Hojat Modaresi
回答by MvanDoorn
Came across this older topic and noticed how everyone is talking about the ProgressDialog. It should be noted that the ProgressDialog is deprecated and the ProgressBar class should be used instead.
遇到了这个较旧的主题,并注意到每个人都在谈论 ProgressDialog。应该注意的是,ProgressDialog 已被弃用,而应使用 ProgressBar 类。
From the android docs:
从 android 文档:
Avoid ProgressDialog
Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. This widget is deprecated because it prevents users from interacting with the app while progress is being displayed. If you need to indicate loading or indeterminate progress, you should follow the design guidelines for Progress & Activityand use a ProgressBarin your layout, instead of using ProgressDialog.
避免 ProgressDialog
Android 包含另一个名为 ProgressDialog 的对话框类,它显示一个带有进度条的对话框。此小部件已被弃用,因为它会阻止用户在显示进度时与应用程序进行交互。如果你需要指出装载或不确定的进度,你应该遵循的设计原则进度及活动,并使用进度的布局,而不是使用ProgressDialog。
回答by RexSplode
There are "indetermined" progress bar, which spins and "determined", whih doesn't. Stick to the latter and you're going to be just fine. To make it "determined", add the progress attribute to it's xml element, set
有“不确定”进度条,它会旋转和“确定”,但不会。坚持后者,你会没事的。要使其“确定”,请将进度属性添加到它的 xml 元素,设置
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
回答by chings228
setAlpha value to 1 when task begin and change back to 0 when task finish but setAlpha only for API Level 11 or above
任务开始时 setAlpha 值为 1,任务完成时变回 0 但 setAlpha 仅适用于 API Level 11 或以上