可以更改TextBox(输入框)下面的线条/边框颜色吗?

28

我正在创建一个在 Android 上基于 Xamarin.Forms 的应用程序,并尝试更改我的 Xamarin.Forms Entry 控件下方的线条颜色。

我的 Entry 控件如下:

<Entry Text="new cool street"/>

enter image description here

我想将此Entry下面的线条颜色从默认的白色更改为紫色,以匹配我的主题。

最好使用Android样式来完成,这样就可以如果可能的话应用于继承自Entry的所有控件

是否可能做到这一点?


用户安卓风格。<item name="colorAccent">@android:color/yourcolour</item> - Akash Amin
@AkashAmin 当 TextBox 没有焦点时, colorAccent 不起作用。我想在 TextBox 失去焦点时改变行颜色。 - JKennedy
10个回答

45
您可以使用自定义渲染器来影响所有条目,
以下是适用于Android的示例:
[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace Android.MyRenderers
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null) return;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                Control.BackgroundTintList = ColorStateList.ValueOf(Color.White);
            else
                Control.Background.SetColorFilter(Color.White, PorterDuff.Mode.SrcAtop);
         }    
    }
}

和iOS:

[assembly: ExportRenderer (typeof(Entry), typeof(MyEntryRenderer))]
namespace iOS.MyRenderers
{
    public class MyEntryRenderer : EntryRenderer
    {
        private CALayer _line;

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);
            _line = null;

            if (Control == null || e.NewElement == null)
                return;

            Control.BorderStyle = UITextBorderStyle.None;

            _line = new CALayer {
                BorderColor = UIColor.FromRGB(174, 174, 174).CGColor,
                BackgroundColor = UIColor.FromRGB(174, 174, 174).CGColor,
                Frame = new CGRect (0, Frame.Height / 2, Frame.Width * 2, 1f)
            };

            Control.Layer.AddSublayer (_line);
        }
    }
}

我对此在Windows上的解决方案不太确定。


你的示例是针对哪个平台的?你知道在哪里可以找到另外两个平台的代码吗? - JKennedy
@user1,这是安卓版,刚刚添加了iOS版本。 - root
谢谢,太好了!如果我找到了,我会看一下Windows并添加它。 - JKennedy
4
我想吻你!谢了,伙计。 - Cícero Moura
你是否遇到了iOS中输入框底部线条宽度的问题? - Akhil George
@Akhil George 请查看下面我的回答 - mahclark

30

我遇到了同样的问题,只需更改 styles.xml 文件(在 Xamarin.Android 项目中)中的 colorAccent 值,即可更改 Entry 字段的光标颜色和底部边框的颜色。

<item name="colorAccent">#BA55D3</item>

看起来只有在控件获得焦点时才会更改颜色。 - CodeGrue
12
聚焦状态:<item name="colorAccent">#FF73B72F</item> 非聚焦状态:<item name="colorControlNormal">#FF73B72F</item> - Xiokraze

10

由于我的内容页面和对话框使用不同的背景颜色,使用样式来指定底部栏颜色完全是错误的答案。而且由于OP只问了关于Android的问题,这只是关于Android的问题...

我使用自定义渲染器将底部栏颜色设置为与文本颜色相同。必须同时使用ElementChanged和PropertyChanged。

[assembly: ExportRenderer(typeof(Xamarin.Forms.Entry), typeof(CustomEntryRenderer))]
namespace XamFormsConnect.Droid
{
    public class CustomEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null && e.NewElement != null)
            {
                var entry = (Xamarin.Forms.Entry)e.NewElement;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                    Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
                else
                    Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == "TextColor")
            {
                var entry = (Xamarin.Forms.Entry)sender;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                    Control.BackgroundTintList = ColorStateList.ValueOf(entry.TextColor.ToAndroid());
                else
                    Control.Background.SetColorFilter(entry.TextColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
        }
    }
}

4
我喜欢这个回答使用TextColor属性而不是像被接受的回答一样硬编码“白色”。 - CodeGrue

5

更新:Xamarin.Forms 2.5

我尝试了最受欢迎的解决方案,但出现错误:“自版本2.5起,上下文已过时。请改用本地上下文。”

经过一些快速搜索:

代码需要更新:

using <YOUR_APP_NAME>.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Content;
using Android.OS;
using Android.Content.Res;
using Android.Graphics;

[assembly: ExportRenderer(typeof(Entry), typeof(MyRenderers))]
namespace Android.MyRenderers
{
    public class MyRenderers : EntryRenderer
    {

        public MyRenderers(Context context) : base(Android.App.Application.Context)
        {

        }
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null) return;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Ivory);
            else
                Control.Background.SetColorFilter(Android.Graphics.Color.Ivory, PorterDuff.Mode.SrcAtop);
        }
    }
}

为需要更新的人提供更新。


2

简单易懂的方法: 在res/values/colors.xml中编辑如下内容: #303F9F

你可以将任何十六进制颜色代码替换#303F9F。

<color name="colorPrimaryDark">#303F9F</color>

我应该在哪里实现这个颜色,以便将其应用于我的“Entry”?到目前为止,这只是定义了一个新的颜色。 - JKennedy
有关我所说的更多信息,请参阅以下网址:https://developer.xamarin.com/guides/android/user_interface/material_theme/ - JKennedy

2

如果有人在使用root的iOS自定义渲染器时遇到宽度或y位置出现问题,我找到了解决方法。

  1. 将该条目和其他两个变量作为类字段包含:
private CustomEntry _entry;
private double _yPos;
private double _width;

OnElementChanged中分配数值:
if (e.NewElement != null)
    _entry = e.NewElement as CustomEntry;
  • OnElementPropertyChanged中,包含以下内容:
  • if (e.PropertyName == "Width")
    {
        _width = _entry.Width;
    }
    else if (e.PropertyName == "Height")
    {
        _yPos = _entry.Height;
    }
    _line.Frame = new CGRect(0, _yPos, _width, 1f);
    

    0
    如果您正在使用Xamarin Forms,请前往Mobile.Droid,资源,值,并在“Your Colur”上进行设置,它将起作用。

    0

    在Android中将输入光标和下划线颜色从粉色更改为其他颜色。只需在Xamarin Forms Android项目的Resources/values文件夹中添加<item name="colorControlActivated">#007bff</item>到styles.xml文件中即可轻松实现。

    注意:此方法将应用更改到应用程序中的每个元素。如果不需要这样,请使用自定义渲染器方法。

    可以参考xamarin论坛上的答案。


    0

    上面的解决方案已经过时了。这个是推荐的解决方案。(同时包含所有必要的使用方法。有时可能会令人困惑...)

    using System;
    using System.ComponentModel;
    using Android.Content;
    using Android.Content.Res;
    using InteriorCircle.Droid.CustomComp;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(Entry), typeof(CustomEntry))]
    namespace InteriorCircle.Droid.CustomComp
    {
        public class CustomEntry : EntryRenderer
        {
            public CustomEntry(Context context) : base(context)
            {
    
            }
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control == null || e.NewElement == null) return;
    
                Control.BackgroundTintList = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Black);
            }
    
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
                if (e.PropertyName == Entry.TextProperty.PropertyName)
                {
                    if (Control.Text.Length > 6)  //this is your condition(For example, here is the length of the text content)
                    {
                        Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Red);
                    }
                    else
                    {
                        Control.BackgroundTintList = ColorStateList.ValueOf(Android.Graphics.Color.Black);
                    }
                }
            }
    
        }
    }
    

    -1

    另一个简单的可视化解决方案是隐藏这行代码:

    <Grid>
       <Entry Placeholder="Your Entry"/>
       <BoxView BackgroundColor="White" HeightRequest="10"/>
    </Grid>
    

    您可以创建自己的组件,而非使用硬编码颜色来改进它。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接