【问题标题】:Xamarin Entry Cursor Position when Selecting All Text选择所有文本时的 Xamarin 条目光标位置
【发布时间】:2021-12-21 17:35:46
【问题描述】:

当我的输入控件获得焦点时,我会选择所有文本。当文本超过入口控件的宽度时,文本的开头将被截断 - 如下图所示:

Beginning truncated

但是,我希望尾部被截断,如下所示:

Tail truncated

<Entry x:Name="entryA" Focused="entry_Focused"/>

private void entry_Focused(object sender, FocusEventArgs e)
{
    entryA.CursorPosition = 0;
    entryA.SelectionLength = entryItem.Text.Length;
}

【问题讨论】:

  • Xamarin 只是告诉本机(android 或 ios)小部件选择是什么。查看 Android 文档,我看不出有任何方法可以在 Android 下控制这种行为。我怀疑 iOS 文本框也缺少这个。 (虽然我没看。)如果 ios 或 android 确实有办法做到这一点,那么答案就是在每个平台上编写一个“自定义渲染器”来设置这样的选项。也许其他人可以在各个平台上找到一种方法来做到这一点。但我对此表示怀疑。

标签: c# xamarin xamarin.forms


【解决方案1】:

通过使用Entry的PropertyChanged方法,可以在数据框中的数据发生变化时进行操作。

我写了一个小例子供大家参考:

这里是 xaml 代码:

<StackLayout>
    <Entry x:Name="entryA" WidthRequest="100" HorizontalOptions="CenterAndExpand" PropertyChanged="entryA_PropertyChanged"></Entry>
</StackLayout>

这是后台代码:

public partial class MainPage : ContentPage
{
    public int len { get; set; }
    public MainPage()
    {
        InitializeComponent();
    }

    private void entryA_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var en = sender as Entry;
        if (en.Text != null && len != en.Text.Length) 
        {
            entryA.CursorPosition = 0;
            len = en.Text.Length;
        }
    }
}

【讨论】:

  • 问题解决了吗?
猜你喜欢
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
  • 2014-08-07
  • 2023-03-13
  • 2013-05-26
  • 2011-12-21
相关资源
最近更新 更多