如何在 Android 中的两个活动之间显示 "Loading" 栏

How to show "Loading" bar between two activities in Android

我是 Android 应用程序编程的新手。我想显示 "loading" 状态或 bar/circle 在我的第一个 activity 中单击一个按钮以在完全加载后转到第二个 activity。

目前,我有两个活动,但加载第二个活动需要一段时间,我希望 bar/circle 帮助用户了解正在加载某些内容,而应用程序尚未加载'它只是坠毁了。

我搜索并发现了许多关于使用 asynctask 从 Web 检索外部数据(如 url 和媒体)的主题,但我认为这不是我的情况:我只需要在单击我的按钮时显示一条非常简单的消息在进入第二个 activity.

之前

在开始第二个 Activity 之前先祝酒词。祝酒词将在活动之间起作用:

在Activity1

 public static Toast transitionToast;

  ..........
  transitionToast.Toast.makeText(this, R.string.loading, Toast.LENGTH_LONG);
  transitionToast.show();
  startActivity(new Intent(this, Activity2.class));

在Activity2:

  public void onStart() {
       super.onStart();
       Activity1.transitionToast.cancel();   // Dismiss the toast     
  }

更好的做法是将 transitionToast 声明放在单独的文件中。

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:visibility="gone"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:text="Click Me" />

</FrameLayout>

然后在按钮的 onclick 侦听器中

ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1);
if(p.getVisibility() != 0){ // check if it is visible
   p.setVisibility(0); // if not set it to visible
   arg0.setVisibility(1 or 2); // use 1 or 2 as parameters.. arg0 is the view(your button) from the onclick listener
}

对于你的第二个问题.. 发生这种情况是因为你已经将按钮的可见性设置为消失或不可见,如果你完成了 activity,那么当你再次调用它时 os 将重新创建它,但如果你没有指定 close 它那么 os 将把它存储在一个叫做 backstack 的东西中,也是一个 activity 有 lifecycle , more 只需复制并粘贴即可

@Override
public void onResume() { // this is called by your activity before it gets visible to the user, when you leave this activity 2 and come back to 
     //activity 1, because activity 1 wasn't killed it is resumed from the backstack and this function is called
    // TODO Auto-generated method stub
    super.onResume();
    // so you will check here, if your button is visible or not like the way you did in the onclick for the progressbar
     ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1); 
     Button b = (Button) findViewById(R.id.button1); // if you have already instantiated your button then nevermind
     if(b.getVisibility() != View.VISIBLE){ // check if it is visible
     //View.VISIBLE is an int variable which is 0
     b.setVisibility(View.VISIBLE); // if not set it to visible
     p.setVisibility(View.INVISIBLE or View.GONE); //View.INVISIBLE is 1, and 2 is View.GONE
     // you can either use the ints or the static variables.. gone stands for when the view is invisible and is not accounted for with respect to space on the screen
     // so if you use relativeLayout and you align a view in respect to another if you use gone your viewlayout will be squashed around,
     // invisible is the when its invisible and is being accounted for
}