如何在Xamarin Forms中设置ListView内的Entry焦点?

6

我有一个列表视图,每行都包含一个标签和一个输入框。如果点击了一行,如何为该输入框设置焦点。我的列表视图是动态生成的。

 void selected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
        {
            return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
        }
        TestReading item = (TestReading)e.SelectedItem;




        //comment out if you want to keep selections
        ListView lst = (ListView)sender;

        lst.SelectedItem = null;

    }

我希望软键盘在用户点击特定行时不论位置何在都能弹出。
1个回答

5
使用 Tapped
   <ListView x:Name="ItemsListView" SeparatorColor="LightGray" BackgroundColor="Green" RowHeight="60">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Tapped="ViewCell_Tapped">
                    <StackLayout Padding="15, 5, 0, 0" Orientation="Horizontal" BackgroundColor="White">
                            <Entry x:Name="myEntry"  HorizontalOptions="FillAndExpand"/>
                            <Label Text = "{Binding ItemText}" FontSize="20" TextColor="Black" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

在代码后端
    private void ViewCell_Tapped(object sender, EventArgs e)
    {
        ViewCell vs = (ViewCell)sender;
        var entry = vs.FindByName<Entry>("myEntry");
        entry?.Focus();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接