我如何回到代码的顶部?
How do I go back to the top of the code?
我一直在学习 C# 并且正在创建一个应用程序。
我已经尝试过 goto 语句,但是标签是 "out of range" 我希望它能把我送回原来的布局,我发现我需要回到代码的顶部。
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 的工作方式。您不能在 'layouts' 之间导航,而是在具有与之关联的布局的活动之间导航。此导航是使用 StartActivity()
方法完成的。
MainActivity
是您的应用程序启动时将显示的第一个 activity。它的关联布局(此处命名为 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
class 中,假设您希望有两个按钮将您带到两个新活动。假设一个 activity 被称为 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 设备有一个专门用于该目的的按钮。但是,如果您必须有一个按钮可以将您带回到之前的 activity,那么只需调用 this.Finish();
就足够了。一旦您为 activity 调用 Finish()
,它就会从活动堆栈的顶部移除,下面的 activity 变得可见。
我一直在学习 C# 并且正在创建一个应用程序。
我已经尝试过 goto 语句,但是标签是 "out of range" 我希望它能把我送回原来的布局,我发现我需要回到代码的顶部。
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 的工作方式。您不能在 'layouts' 之间导航,而是在具有与之关联的布局的活动之间导航。此导航是使用 StartActivity()
方法完成的。
MainActivity
是您的应用程序启动时将显示的第一个 activity。它的关联布局(此处命名为 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
class 中,假设您希望有两个按钮将您带到两个新活动。假设一个 activity 被称为 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 设备有一个专门用于该目的的按钮。但是,如果您必须有一个按钮可以将您带回到之前的 activity,那么只需调用 this.Finish();
就足够了。一旦您为 activity 调用 Finish()
,它就会从活动堆栈的顶部移除,下面的 activity 变得可见。