After some research, I managed to do this with effects.
Reducing is what you should do it in the Android project and in the iOS project separately.
Like Jason , this can be done with custom renders. But still you have to implement it in every Android / iOS project.
Android , , :
. , , / ic_clear_icon.png.
public class ClearEntryEffect : PlatformEffect
{
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;
}
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);
}
}
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);
}
}
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 (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width()))
{
editText.Text = string.Empty;
return true;
}
}
}
return false;
}
}
iOS . :
public class ClearEntryEffect : PlatformEffect
{
protected override void OnAttached()
{
ConfigureControl();
}
protected override void OnDetached()
{
}
private void ConfigureControl()
{
((UITextField)Control).ClearButtonMode = UITextFieldViewMode.WhileEditing;
}
}
PCL, ClearEntryEffect ( Android/iOS).
, XAML, .
public class ClearEntryEffect : RoutingEffect
{
public ClearEntryEffect() : base("Effects.ClearEntryEffect")
{
}
}
(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>