将焦点设置为 Xamarin.Forms 中的条目

问题描述:

这只是一个简化的示例,但我正在尝试进行设置,以便当我在我的应用程序中打开此页面时,发生的第一件事是键盘弹出准备好让用户输入他们的响应到一个条目字段.

This is just a simplified example, but I'm trying to set this up so that when I open up this page in my Application, the first thing that happens is the keyboard pops up ready for the user to type in their response to an Entry field.

    var namelabel = new Label { Text = "What is your name?" };
    var nameentry = new Entry { Placeholder = "Type here..." };
    var colorlabel = new Label { Text = "What's your fav color?" };
    var colorentry = new Entry { Placeholder = "Type here..." };
    Content = new StackLayout {
       Spacing = 15,
       Children = { namelabel, nameentry, colorlabel, colorentry }
    };

如何将页面的焦点设置为第一个条目?此外,在用户输入他们的第一个条目后,我如何设置它以便用户可以按下键盘上的下一步"按钮(或类似的东西),应用程序将带他们填写第二个条目?

How can I set the focus of the page to the first entry? And additionally, after the user has put in their first entry, how could I set this up so that the user could press a "Next" Button on the Keyboard (or something along those lines) and the app will take them to fill in the second entry?

使用Focus方法

nameentry.Focus();

如果您希望在页面出现时设置焦点,您可能应该在 OnAppearing 方法中执行此操作

If you want the focus to be set when your page appears, you should probably do this in the OnAppearing method

    protected override void OnAppearing ()
    {
        base.OnAppearing ();

        nameentry.Focus();
    }