简体   繁体   English

如何返回代码顶部?

[英]How do I go back to the top of the code?

I have been learning C# and I am creating an app. 我一直在学习C#,并且正在创建一个应用程序。

I have tried goto statements however the labels are "out of range" I want it to send me back to the original layout which I found out I need to go back to the top of the code. 我尝试了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 };
            };
            };
            };
            }
            }
            }

The way you are trying is not how Android works. 您尝试的方式不是Android的工作方式。 You cannot navigate between 'layouts', instead you navigate between activities which have a layout associated with them. 您无法在“版式”之间导航,而是在具有与其关联的布局的活动之间导航。 This navigation is done using the StartActivity() method. 此导航使用StartActivity()方法完成。

The MainActivity is the first activity that will be shown when your app launches. MainActivity是启动应用程序时将显示的第一个活动。 It's associated layout (which is named as Main here) should have only one button that will take you to the next screen. 它的关联布局(在这里被命名为Main )应该只有一个按钮,它将带您进入下一个屏幕。 Suppose its ID is btnForActivity2 . 假设其ID为btnForActivity2 Your OnCreate for MainActivity will be like this: 您的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));
    };
}

In the MyActivity2 class, let's say you want to have two buttons to take you to two new activities. MyActivity2类中,假设您希望有两个按钮将您带到两个新活动。 Assume one activity is called MyActivity3 and the other is called MyActivity4 . 假设一个活动称为MyActivity3 ,另一个活动称为MyActivity4 In this case, the OnCreate method of your MyActivity2 will be something like this: 在这种情况下,您的MyActivity2OnCreate方法将如下所示:

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));
    };
}

This is how basic navigation works in Android. 这就是Android中基本导航的工作方式。

By the way, you may have noticed that I have not talked about a Back button. 顺便说一句,您可能已经注意到我还没有谈论过“后退”按钮。 It's because I have never explicitly implemented it as, unlike iOS devices, Android devices have a button specifically for that purpose. 这是因为我从未明确实现它,因为与iOS设备不同,Android设备具有专门用于此目的的按钮。 However, if you must have a button that takes you back to the previous activity then simply calling this.Finish(); 但是,如果您必须具有一个使您返回上一活动的按钮,则只需调用this.Finish(); should suffice. 应该足够了。 Once you call Finish() for an activity, it is removed from the top of the activities stack and the activity below becomes visible. 一旦为活动调用Finish() ,它就会从活动堆栈的顶部删除,并且下面的活动变为可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM