我正在为一个条目创建自定义渲染器。我主要是尝试在输入字段的左侧添加一个图标。我已经在Android中使用SetCompoundDrawablesRelativeWithIntrinsicBounds((和iOS中的LeftView来实现了这一点。
当 ClearButtonVisibility 设置为"编辑时"时,会出现此问题。在条目中键入内容并单击清除按钮后,左侧图标将与文本一起清除(这仅在 Android 中发生。在 iOS 中,仅清除文本。我意识到内置的透明按钮图像位于复合可绘制对象中,因此我正在使用我的图标和内置图标设置复合可绘制对象。
最好的解决方法是什么?我希望它,以便在单击清除按钮时,仅清除文本而不是图标。
注意:该图标会暂时清除。触发任何其他事件时,图标将返回。
自定义条目 - 单击之前
自定义条目 - 单击后
自定义条目 - 在某些事件之后
Android.Renderer:
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Styles.Android.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// update control UI
UpdateControl();
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
// update control UI
UpdateControl();
base.OnElementChanged(e);
}
private void UpdateControl()
{
if (Element == null || Control == null)
return;
var entry = Element as CustomEntry;
// set border/background
var gradientBackground = new GradientDrawable();
gradientBackground.SetShape(ShapeType.Rectangle);
gradientBackground.SetColor(Element.BackgroundColor.ToAndroid());
gradientBackground.SetCornerRadius(entry.CornerRadius);
gradientBackground.SetStroke(entry.BorderWidth, entry.BorderColor.ToAndroid());
Control.SetBackground(gradientBackground);
// set appropriate icon
int resID = Resources.GetIdentifier("icon_image", "drawable", this.Context.PackageName);
var drawable = ContextCompat.GetDrawable(this.Context, resID);
Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, Control.GetCompoundDrawables()[2], null);
Control.CompoundDrawablePadding = entry.IconPadding;
paddingLeft = entry.IconPadding;
// set padding
Control.SetPadding(Control.PaddingLeft, Control.PaddingTop, Control.PaddingRight, Control.PaddingBottom);
}
}
}
您可以覆盖OnTouchListener
以清理文本并再次SetCompoundDrawable
。
更改自定义渲染器,如下所示:
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Styles.Android.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
static Drawable drawableLeft;
static Drawable drawableRight;
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// update control UI
UpdateControl();
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
// update control UI
UpdateControl();
base.OnElementChanged(e);
}
private void UpdateControl()
{
if (Element == null || Control == null)
return;
var entry = Element as CustomEntry;
// set border/background
var gradientBackground = new GradientDrawable();
gradientBackground.SetShape(ShapeType.Rectangle);
gradientBackground.SetColor(Element.BackgroundColor.ToAndroid());
gradientBackground.SetCornerRadius(entry.CornerRadius);
gradientBackground.SetStroke(entry.BorderWidth, entry.BorderColor.ToAndroid());
Control.SetBackground(gradientBackground);
// set appropriate icon
int resID = Resources.GetIdentifier("icon_image", "drawable", this.Context.PackageName);
drawableLeft = ContextCompat.GetDrawable(this.Context, resID);
drawableRight = Control.GetCompoundDrawables()[2];
Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(drawableLeft, null, drawableRight , null);
Control.CompoundDrawablePadding = entry.IconPadding;
paddingLeft = entry.IconPadding;
// set padding
Control.SetPadding(Control.PaddingLeft, Control.PaddingTop, Control.PaddingRight, Control.PaddingBottom);
Control.SetOnTouchListener(new OnDrawableTouchListener());
}
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;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(drawableLeft, null, drawableRight, null);
if (drawableRight != null)
{
bool touchable = e.GetX() > (editText.Width
- editText.PaddingRight - drawableRight.IntrinsicWidth)
&& (e.GetX() < ((editText.Width - editText.PaddingRight)));
if (touchable)
{
Console.WriteLine(editText.Text);
editText.Text = "";
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(drawableLeft, null, drawableRight, null);
return true;
}
}
}
return false;
}
}
}
}