【问题标题】:How to set Focus to entry in xamarin forms如何将焦点设置为 xamarin 表单中的条目
【发布时间】:2019-07-04 20:15:34
【问题描述】:

我想将focus() 添加到我的输入字段中,当我添加entry.Focus() 时,它在页面顶部的条目中可以正常工作。但是当到达底部时它不能顺利工作。而且我尝试使用SoftInput 方法,但它隐藏了底部的条目,并且当页面只有一个或两个条目时,设计也在发生变化。

请帮帮我

【问题讨论】:

  • 所以你的问题不是焦点,而是键盘隐藏了输入对吗?
  • 实际上我想关注下一个条目,当我从上一个条目中按键盘选项中的完成时。

标签: xamarin xamarin.forms xamarin.android xamarin.ios focus


【解决方案1】:

@AbbyWang 的答案是正确的,你也可以使用ReturnCommand 属性。

<StackLayout
    Orientation="Vertical">
    <Entry
        x:Name="firstLabel"
        ReturnType="Next"
        FontSize="Small"
        TextColor="Black" />
    <Entry
        x:Name="secondLabel"
        ReturnType="Done"
        FontSize="Small"
        TextColor="Black" />
</StackLayout>

以及背后的代码

public YourPage()
{
    InitializeComponent();

    this.firstLabel.ReturnCommand = new Command(() => this.secondLabel.Focus());

    // or you can use this to call a command on your viewmodel
    this.secondLabel.ReturnCommand = new Command(() => YourViewModel.SomeCommand.Execute(null));
}

【讨论】:

    【解决方案2】:

    如果您的页面上只有两个条目,您只需将Completed 事件添加到顶部条目,并在此事件中调用entry.Focus() 以获得底部条目。所以代码是这样的:

    MainPage.xaml:

    <StackLayout>
            <StackLayout Orientation="Horizontal" VerticalOptions="Start">
                <!-- Place new controls here -->
                <Entry Text="top" x:Name="TopEntry"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                Completed="Entry_Completed"        />
            </StackLayout>
    
    
            <StackLayout Orientation="Horizontal" VerticalOptions="End">
                <!-- Place new controls here -->
                <Entry Text="Bottom"  x:Name="BottomEntry"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand" />
            </StackLayout>
    </StackLayout>
    

    MainPage.xaml.cs

    void Entry_Completed(object sender, EventArgs e)
    {
        BottomEntry.Focus();
    }
    

    如果您有超过 2 个条目,并且想要在按键盘上的“完成”时专注于下一个条目,您也可以参考this

    【讨论】:

    • 一个小建议,尽量减少代码的过度包装,这将使应用程序运行顺畅且重量更轻,在您的示例代码上,只需一个StackLayout 就足够了:)
    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多