如何在 android 中启动和停止进度条?

How to start and stop progressbar in android?

我有一个 activity 调用第二个 java class。我想在调用第二个 class 后显示进度条,然后 return 正常执行 activity。我找到了一些其他线程,但我无法让进度条停止。

非常简单:

显示进度

ProgressDialog dialog = ProgressDialog.show(getContext(), "Title", "Message");

并阻止它:

dialog.dismiss();

here 上有一个完整的例子。

引用:

Declare your progress dialog:

ProgressDialog progress;

When you're ready to start the progress dialog:

progress = ProgressDialog.show(this, "dialog title",
    "dialog message", true);

and to make it go away when you're done:

progress.dismiss();

Here's a little thread example for you:

// Note: declare ProgressDialog progress as a field in your class.

progress = ProgressDialog.show(this, "dialog title",
  "dialog message", true);

new Thread(new Runnable() {
  @Override
  public void run()
  {
    // do the thing that takes a long time

    runOnUiThread(new Runnable() {
      @Override
      public void run()
      {
        progress.dismiss();
      }
    });
  }
}).start();

ProgressDialog 已弃用,因此您可能需要使用 ProgressBar。

我发现 this post 删除其中一个。

Well, I think this is rather ridiculous, but here is how I fixed it.

In my xml for the ProgressBar, I added android:visibility="gone" to hide it by default. Then, in my code, I first told it to display (View.VISIBLE) before it tried getting the server list, then I told it to hide (View.GONE) after it was done. This worked (I could see the progress indicator while the data loaded, then it went away). So I suppose I couldn't get it to hide in the code because the code is not what forced it to be visible to begin with... That seems like a bug to me.