我是Android应用程序编程的新手。我想在我的FIRST活动中点击按钮以显示“加载”状态或条形/圆圈,以便在完全加载后转到SECOND活动。
目前,我有两个活动但是需要一段时间才能加载第二个活动,我想要那个条形/圆圈来帮助用户理解加载的东西以及应用程序还没有崩溃。
我已经搜索并发现了很多关于使用asynctask从网络中检索外部数据(如网址和媒体)的主题,但我认为这不是我的情况:在转到第二个活动之前单击我的按钮时,我只需要一个非常简单的消息
答案 0 :(得分:2)
<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
}
对于你的第二个问题..这是因为你已经将按钮的可见性设置为不可见,如果你完成了活动,那么当你再次调用它时,操作系统会重新创建它,但如果你没有&# 39; t指定关闭它然后操作系统会将其存储在名为 backstack 的内容中,活动还有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
}
答案 1 :(得分:1)
在开始第二个活动之前显示Toast。吐司将在活动之间起作用:
在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声明放在一个单独的文件中。