[英]How do I go back to the top of the code?
我一直在学习C#,并且正在创建一个应用程序。
我尝试了goto语句,但是标签“超出范围”,我希望它使我回到原来的布局,发现我需要回到代码顶部。
namespace TheAppOfJack
{
[Activity(Label = "The App Of Jack", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.MyButton);
button1.Click += delegate { button1.Text = string.Format("You have clicked this random button {0} times ( ͡° ͜ʖ ͡°)", count++); };
Button button2 = FindViewById<Button>(Resource.Id.button1);
button2.Click += delegate
{
SetContentView(Resource.Layout.Gallery);
Button button3 = FindViewById<Button>(Resource.Id.back);
button3.Click += delegate { SetContentView(Resource.Layout.Main); };
Button button4 = FindViewById<Button>(Resource.Id.next1);
button4.Click += delegate
{
SetContentView(Resource.Layout.Gallery2);
button3.Click += delegate { SetContentView(Resource.Layout.Main); };
Button button5 = FindViewById<Button>(Resource.Id.next2);
button5.Click += delegate
{
SetContentView(Resource.Layout.Gallery3);
Button button6 = FindViewById<Button>(Resource.Id.home);
button6.Click += delegate {
//this is where i want the code to send me back to the top };
};
};
};
}
}
}
您尝试的方式不是Android的工作方式。 您无法在“版式”之间导航,而是在具有与其关联的布局的活动之间导航。 此导航使用StartActivity()
方法完成。
MainActivity
是启动应用程序时将显示的第一个活动。 它的关联布局(在这里被命名为Main
)应该只有一个按钮,它将带您进入下一个屏幕。 假设其ID为btnForActivity2
。 您的MainActivity
OnCreate
将如下所示:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button btn = FindViewById<Button>(Resource.Id.btnForActivity2);
btn.Click += delegate
{
StartActivity (typeof(MyActivity2));
};
}
在MyActivity2
类中,假设您希望有两个按钮将您带到两个新活动。 假设一个活动称为MyActivity3
,另一个活动称为MyActivity4
。 在这种情况下,您的MyActivity2
的OnCreate
方法将如下所示:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MyActivity2Layout);
Button btn = FindViewById<Button>(Resource.Id.btnForActivity3);
btn.Click += delegate
{
StartActivity (typeof(MyActivity3));
};
btn = FindViewById<Button>(Resource.Id.btnForActivity4);
btn.Click += delegate
{
StartActivity (typeof(MyActivity4));
};
}
这就是Android中基本导航的工作方式。
顺便说一句,您可能已经注意到我还没有谈论过“后退”按钮。 这是因为我从未明确实现它,因为与iOS设备不同,Android设备具有专门用于此目的的按钮。 但是,如果您必须具有一个使您返回上一活动的按钮,则只需调用this.Finish();
应该足够了。 一旦为活动调用Finish()
,它就会从活动堆栈的顶部删除,并且下面的活动变为可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.