xamarin.forms如何选择所有文本的焦点与输入?

hl0ma9xz  于 2023-02-06  发布在  其他
关注(0)|答案(3)|浏览(182)

我知道Xamarin.Android的代码,但是Xamarin.IOS怎么样?
Xamarin.安卓系统代码:

public class CustomNumEntry : EntryRenderer
{
    public CustomNumEntry(Context context) : base(context)
    {
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (this.Control != null)
        {
            Control.SetPadding(10, 0, 0, 15);
            Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagSigned | Android.Text.InputTypes.NumberFlagDecimal;
            Control.TextAlignment = Android.Views.TextAlignment.Center;

        }
        if (e.OldElement == null)
        {
            var nativeEditText = (global::Android.Widget.EditText)Control;
            nativeEditText.SetSelectAllOnFocus(true);
        }

    }
}
50pmv0ei

50pmv0ei1#

对于Xamarin iOS:

nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                   {
                      nativeTextField.PerformSelector(new Selector("selectAll"), null, 0.0f);
                   };

使用iOS自定义渲染器的表单(您正在寻找):

public class CustomNumEntry : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);    
            if (Control != null)
            {
                var nativeTextField = (UITextField)Control;    
                nativeTextField.EditingDidBegin += (object sender, EventArgs eIos) =>
                {
                   nativeTextField.PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
                };                           
            }
        }

    }
4c8rllxm

4c8rllxm2#

    • 解决方案:**

您可以在方法**EditingStarted**中实现它。

namespace xxx.iOS
{
  public class MyEntryRenderer:EntryRenderer,IUITextFieldDelegate
  {
    public MyEntryRenderer()
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if(Control!=null)
        {
            Control.WeakDelegate = this;   

        }

    }

    [Export("textFieldDidBeginEditing:")]
    public void EditingStarted(UITextField textField)
    {
        textField.PerformSelector(new Selector("selectAll:"), null, 0.0f);
    }

  }
}
1dkrff03

1dkrff033#

以下是通过此post实现的解决方案:

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

示例显示了在焦点上选择所有文本,但您也可以根据需要做同样的事情。

相关问题