Xamarin表单:添加清除条目

adS*_*Sad 5 xamarin xamarin.forms

您能为我指明正确的方向吗:我如何在Xamarin中实现。形成一个清晰的带有行为的输入按钮。行为将是:当点击以清除图标时,Android和iOS上条目的内容将被删除。

默认情况下,进入控件没有此控件。

在此处输入图片说明

结果将是:

在此处输入图片说明

adS*_*Sad 5

经过一些研究,我设法做到了效果。

缩小尺寸是您必须分别在Android项目和iOS项目上执行此操作。

就像Jason建议的那样,也可以使用自定义渲染器来完成。但是仍然必须在每个Android / iOS项目上实现它。

在android项目上,您可以通过添加如下所示的效果来做到这一点:

注意:在共享代码之前,我必须通知您,您必须在“资源” /图形中添加一个图标,名称为ic_clear_icon.png。

/// <summary>
    /// Adding a clear entry effect.
    /// </summary>
    public class ClearEntryEffect : PlatformEffect
    {
        /// <summary>
        /// Attach the effect to the control.
        /// </summary>
        protected override void OnAttached()
        {
            ConfigureControl();
        }

        protected override void OnDetached()
        {
        }

        private void ConfigureControl()
        {
            EditText editText = ((EditText)Control);
            editText.AddTextChangedListener(new OnTextChangedListener(editText));
            editText.FocusChange += EditText_FocusChange;
        }

        /// <summary>
        /// If the entry looses focus, delete the x icon.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditText_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)
        {
            var editText = (EditText)sender;
            if (e.HasFocus == false)
                editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
        }
    }

    /// <summary>
    /// Adding an OnTextChangedListener to my entry.
    /// </summary>
    public class OnTextChangedListener : Java.Lang.Object, Android.Text.ITextWatcher
    {
        private EditText _editText;
        public OnTextChangedListener(EditText editText)
        {
            _editText = editText;
        }
        public void AfterTextChanged(IEditable s)
        {
        }

        public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
        {
        }

        public void OnTextChanged(ICharSequence s, int start, int before, int count)
        {
            if (count != 0)
            {
                _editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.ic_clear_icon, 0);
                _editText.SetOnTouchListener(new OnDrawableTouchListener());
            }
            else
                _editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
        }
    }

    /// <summary>
    /// Adding a Touch listener so it can be clicked in order to remove the text.
    /// </summary>
    public class OnDrawableTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
    {
        public bool OnTouch(Android.Views.View v, MotionEvent e)
        {
            if (v is EditText && e.Action == MotionEventActions.Up)
            {
                EditText editText = (EditText)v;

                if (editText.Text != null)
                    editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.ic_clear_icon, 0);

                if (editText.GetCompoundDrawables()[2] != null)
                {
                    //If the region on which i tapped is the region with the X the text will be cleaned
                    if (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width()))
                    {
                        editText.Text = string.Empty;
                        return true;
                    }
                }
            }

            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在iOS项目方面更简单。因为它本身具有:

 public class ClearEntryEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            ConfigureControl();
        }

        protected override void OnDetached()
        {
        }

        private void ConfigureControl()
        {
            ((UITextField)Control).ClearButtonMode = UITextFieldViewMode.WhileEditing;
        }
    } 
Run Code Online (Sandbox Code Playgroud)

现在,您可以在PCL项目中创建一个效果类,并引用两个ClearEntryEffect类(来自Android / iOS项目)。

此效果类是必需的,因此可以在声明Entry的XAML文件中对其进行引用。

public class ClearEntryEffect : RoutingEffect
    {
       public ClearEntryEffect() : base("Effects.ClearEntryEffect")
        {
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在,您只需在共享表单项目(在我的情况下为PCL)中将其引用到xaml中:

1)引用效果所在的名称空间:xmlns:effects =“ clr-namespace:YourNamespace.Common.Effects”

2)在条目中添加效果:

<Entry x:Name="OrderNo"
Text="{Binding OrderNo, Mode=TwoWay}"
   <Entry.Effects>
       <effects:ClearEntryEffect/>
   </Entry.Effects>
</Entry>
Run Code Online (Sandbox Code Playgroud)


son*_*ler 5

这适用于 Android 和 iOS。

这是我的 xaml。

<Grid>
   <Entry x:Name="search" TextChanged="SearchChanged" Placeholder="Search"/>
   <Image x:Name="clearSearch" Source="delete.png" HeightRequest="16" WidthRequest="16" HorizontalOptions="End">
         <Image.GestureRecognizers>
               <TapGestureRecognizer Tapped="OnSearchTap" NumberOfTapsRequired="1" />
         </Image.GestureRecognizers>
   </Image>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这是我的 c#

    private void OnSearchTap(object sender, EventArgs args)
    {
        search.Text = "";
    }
Run Code Online (Sandbox Code Playgroud)