简体   繁体   English

如何使程序循环到顶部?

[英]How can I get the program to loop to the top?

OK, I am very new to Android and C#. 好的,我是Android和C#的新手。 Just started today in fact. 其实今天才刚开始。 I normally program in VB, but anyway I was trying MonoDroid out and after a couple of small tutorials I tried something on my own. 我通常使用VB进行编程,但是无论如何我都尝试使用MonoDroid,并且在进行了一些小型教程之后,我自己尝试了一些方法。 What I want is that there are two radio buttons on the canvas. 我想要的是在画布上有两个单选按钮。 And a disabled button, it only gets enabled when you click on one of the radio buttons. 还有一个禁用的按钮,仅当您单击其中一个单选按钮时才会启用。 Funnily, you have to code the radio buttons to uncheck when the other one is clicked unlike in windows forms, or I'm missing someting. 有趣的是,您必须对单选按钮进行编码,以取消选中与Windows窗体不同的单击其他窗体的时间,或者我遗漏了一些东西。 But I managed that. 但是我做到了。 And when you press the button 'Next' it goes to the next page. 然后,当您按下“下一步”按钮时,它将转到下一页。

This is my code for the above: 这是我上面的代码:

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        RadioButton radSilent1 = FindViewById<RadioButton>(Resource.Id.radSilent);
        RadioButton radVibrate1 = FindViewById<RadioButton>(Resource.Id.radVibrate);
        Button button1 = FindViewById<Button>(Resource.Id.btnNext);
        radSilent1.Click += delegate
        {
            button1.Enabled = true;
            if (radSilent1.Checked == true)
                radVibrate1.Checked = false;
            else if (radVibrate1.Checked == true)
                radSilent1.Checked = false;
            {
            }
        };
        radVibrate1.Click += delegate
        {
            button1.Enabled = true;
            if (radVibrate1.Checked == true)
                radSilent1.Checked = false;
            else if (radSilent1.Checked == true)
                radVibrate1.Checked = false;
            {
            }
        };
        // Set our view from the "secondry" layout resource
        button1.Click += delegate { SetContentView(Resource.Layout.Secondry); };
    }

This brings up the second canvas. 这将弹出第二个画布。 Where I have another button 'Back'. 我还有另一个按钮“返回”。 When I press that button it brings me to the first screen but the above code doesn't work. 当我按下该按钮时,它带我到第一个屏幕,但是上面的代码不起作用。 I press the two radio buttons and both are checked, and the button doesn't enable either. 我按下了两个单选按钮,并且两个按钮均已选中,并且该按钮均未启用。 Why does this happen? 为什么会这样? Remember I am very new to this sorry. 记住我很抱歉。 lol Any help is much appreciated. 大声笑任何帮助是非常感谢。

Thanks. 谢谢。

The error is that when you have used new layout ( SetContentView(Resource.Layout.Secondry) ) you have lost all attached event handlers that was initialized in OnCreate(Bundle bundle) . 错误是,当您使用新的布局( SetContentView(Resource.Layout.Secondry) )时,您丢失了所有在OnCreate(Bundle bundle)初始化的附加事件处理程序。 To solve this problem you need to create 2 methods something like InitializeMainView() and InitializeSecondView() where attach handlers to controls events on selected layout. 要解决此问题,您需要创建2种方法,如InitializeMainView()InitializeSecondView() ,在其中将处理程序附加到控件以控制选定布局上的事件。 And after changing layout you just call init method for selected view. 更改布局后,您只需为选定的视图调用init方法。

But i think that the best solution will be to create new separate activity for second view. 但是我认为最好的解决方案是为第二个视图创建新的单独活动。

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

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