[英]Change focus color of Entry control in Xamarin Forms
如何在 Xamarin 表单的 Entry 控件中更改焦点边框和光标颜色? 在模拟器中它是标准的红色?
我在我的 Android 项目中添加了这个
[assembly: ExportRenderer(typeof(CustomEntryControl), typeof(MyEntryRenderer))]
namespace MyApp.Droid
{
public class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
}
}
}
}
但是找不到边框或光标的属性?
试试下面的代码
public class EntryCustomRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetBackgroundColor(global::Android.Graphics.Color.Transparent);
// set the cursor color the same as the entry TextColor
IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
IntPtr mCursorDrawableResProperty =
JNIEnv.GetFieldID(IntPtrtextViewClass, "mCursorDrawableRes", "I");
// replace 0 with a Resource.Drawable.my_cursor
JNIEnv.SetField(Control.Handle, mCursorDrawableResProperty, 0);
}
}
}
请注意,如果您为 Entry 设置了 TextColor,则如果您将资源 id 的值保留为“0”,则光标将使用该颜色
嗨,试试这个代码来改变 iOS 条目看起来像原生 Android 条目在你的 ios 部分添加这个渲染器; 焦点和非焦点颜色也在变化。
记住在命名空间上方添加这一行 [程序集:ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
public class CustomEntryRenderer : EntryRenderer
{
private CALayer _borderLayer;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null)
return;
Control.BorderStyle = UITextBorderStyle.None;
var element = Element as Entry;
if (element == null)
return;
//DrawBorder(element.BorderColor.ToCGColor());
DrawBorder(UIColor.FromRGB(156, 156, 156));
e.NewElement.Unfocused += (sender, evt) =>
{
DrawBorder(UIColor.FromRGB(156, 156, 156)); // unfocused, set color
};
e.NewElement.Focused += (sender, evt) =>
{
DrawBorder(UIColor.FromRGB(245, 0, 47)); ; // focused, set color
};
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var element = Element as Entry;
if (element == null)
return;
DrawBorder(UIColor.FromRGB(156, 156, 156));
}
public override CGRect Frame
{
get { return base.Frame; }
set
{
base.Frame = value;
var element = Element as Entry;
if (element == null)
return;
// DrawBorder(element.BorderColor.ToCGColor());
DrawBorder(UIColor.FromRGB(156, 156, 156));
}
}
private void DrawBorder(UIColor borderColor)
{
if (Frame.Height <= 0 || Frame.Width <= 0)
return;
if (_borderLayer == null)
{
_borderLayer = new CALayer
{
MasksToBounds = true,
Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f),
BorderColor = borderColor.CGColor,
BorderWidth = 1.0f
};
Control.Layer.AddSublayer(_borderLayer);
Control.BorderStyle = UITextBorderStyle.None;
}
else
{
_borderLayer.BorderColor = borderColor.CGColor;
_borderLayer.Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.