android - 如何在 Android 中的两个 Activity 之间显示 "Loading"栏

标签 android android-activity progress-bar loading

我是 Android 应用程序编程的新手。我想在我的第一个 Activity 中单击按钮以在完全加载后转到第二个 Activity 时显示“正在加载”状态或条形/圆圈。

目前,我有两个 Activity,但加载第二个 Activity 需要一段时间,我希望该条形/圆圈可以帮助用户了解正在加载某些内容,并且应用程序并没有崩溃。

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

最佳答案

<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 ,那么当你再次调用它时操作系统将重新创建它,但如果你没有' t 指定关闭它然后操作系统会将其存储在称为 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
}

关于android - 如何在 Android 中的两个 Activity 之间显示 "Loading"栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28080628/

相关文章:

java - 如何在 Java 中更新 Swing JProgressBar

java - 将 SWT 进度条切换为不确定(替换小部件)

android - 通过 Google Fit Android API 检索的每周步数与 Google Fit 官方应用中显示的步数不匹配

java - Android从另一个类更改TextView文本

android - 即使我得到另一个实例,也可以从正确的线程进行 Realm 访问

java - 安卓 Java : Understanding Activity Life?

android - 如何将 ArrayList< com.google.api.services.drive.model.File> 传递给 Activity

C# Winform ProgressBar 和 BackgroundWorker

android - 如何在运行时修改xml中的颜色属性?

java - 用独特的字符串填充微调器