我有一个listview,每一行都包含一个标签和一个条目。如果点击了行,如何设置条目的焦点。我的 ListView 是动态生成的。

 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;

    }

我希望每当用户轻按特定行时,无论其处于任何位置,都将显示软键盘。

最佳答案

使用点击

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

09-16 05:14
查看更多