如何在Android两个活动之间显示“加载”进度条

7
我是一名新手 Android 应用程序开发者。我想在点击我的第一个活动中的按钮后,在完全加载后显示“正在加载”状态或圆形/条形图,以便前往第二个活动。
目前,我有两个活动,但需要一段时间才能加载第二个活动,并且我希望该进度条/圆形图可以帮助用户了解有东西正在加载并且应用程序没有崩溃。
我已经搜索并找到了许多关于使用异步任务从 Web 检索外部数据(如 URL 和媒体)的主题,但我认为这不适用于我:我只需要在单击我的按钮之前向用户显示非常简单的消息,然后转到第二个活动。

1
在你的XML文件中创建一个FrameLayout,然后添加一个ProgressBar和你要点击的按钮。接着,在你的OnClickListener中通过setVisibility(View.Visible)来切换你的ProgressBar和按钮的可见性,对于你的ProgressBar设置为可见,对于你的按钮则设置为不可见,就这样。 - Elltz
1
@Elltz,听起来像是一个答案。 - Nathan Tuggy
非常感谢您的回答,能否提供一个代码示例?抱歉,我对此非常陌生。 - Olileo
2个回答

3
<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
}

针对你的第二个问题...发生这种情况是因为你将该按钮的可见性设置为gone或invisible。当你完成该活动后,如果再次调用它,操作系统将重新创建它,但如果你没有指定关闭它,则操作系统会将其存储在一个称为backstack的东西中。此外,一个活动有其生命周期更多信息可以直接复制粘贴。

@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
}

非常感谢Eltz抽出时间来回答我的问题。将来,我会做更多的研究和学习,希望能够帮助初学者 :) - Olileo
好的,先生,但请检查并运行它,如果它能正常工作,那么您再接受它。@Olileo - Elltz
谢谢,它可以运行,但进度条会冻结,我不知道为什么...此外,当我在Activity2中并通过“上一个按钮”回到Activity1时,它会显示我的进度条无限移动。我不知道是否犯了错误?再次感谢 - Olileo
@Olileo 先生,您是否正在使用asynctask?如果没有,您可能想要尝试一下或者尝试使用线程,它们非常容易使用。卡顿或冻结的原因可能是您正在做一些耗时的工作,这往往会导致进度条冻结。您知道您的应用程序运行在一个称为UI线程或主线程的线程上,广播接收器和操作系统为帮助您的应用程序工作所做的任何事情都是在那里完成的。因此,如果您想要做更多的工作,您可以创建一个不同的工作路径,并在完成后将其带回到UI线程。为了证明我的话,当您单击按钮时,它会流畅地运行,然后冻结,但当您返回时,它是否会冻结呢? - Elltz
是的,先生,请尝试以下链接:123谷歌搜索 @Olileo - Elltz
显示剩余2条评论

1
在开始第二个Activity之前显示一个Toast。Toast可以在两个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 的声明放在一个单独的文件中。

非常感谢。我添加了公共静态Toast并导入Toast小部件,根据您的说法,我应该在哪里放置代码?谢谢。 - Olileo
感谢您的时间,氰化物。 - Olileo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接