在Xamarin Forms中更改Entry控件的焦点颜色

时间:2017-09-19 12:59:19

标签: c# android xaml xamarin.forms custom-controls

如何在Xamarin表单的Entry控件中更改焦点边框和光标颜色?在模拟器中它是标准红色?

我在我的Android项目中添加了这个

body{min-height:calc(100vh)}

但是找不到边框或光标的属性?

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以在Android Project中的style.xml文件中更改Entry焦点颜色。路径是:Resources / values / styles.xml

然后,查看“ colorAccent”属性以设置自定义颜色。

ColorAccent in Xamarin Android Project

答案 1 :(得分:0)

尝试以下代码

  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”值,则光标将使用该颜色

答案 2 :(得分: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);
        }
    }
}