【问题标题】:Xamarin Forms and Material design, how to change hint colorXamarin Forms and Material design,如何更改提示颜色
【发布时间】:2018-06-09 15:56:35
【问题描述】:

我尝试为 Xamarin Forms 创建 Material Design Entry,但提示颜色和底线有问题。

编辑 York Shen 答案:

Xaml 页面

<customControls:CustomEntry
    Keyboard="Email"
    Placeholder="EMAIL"
    Style="{StaticResource EntryAccount}"
    Text="{Binding Email}" />

EntryAccount 样式

<Style x:Key="EntryAccount" TargetType="customControls:CustomEntry">
    <Setter Property="PlaceholderColor" Value="{StaticResource BackgroundColor}" />
    <Setter Property="BottomLineColor" Value="{StaticResource BackgroundColor}" />
    <Setter Property="TextColor" Value="{StaticResource BackgroundColor}" />
    <Setter Property="FontSize" Value="{StaticResource Subtitle1}" />
    <Setter Property="FontFamily" Value="{StaticResource Subtitle1Font}" />
</Style>

自定义条目

public class CustomEntry : Entry
{
    public static readonly BindableProperty BottomLineColorProperty =
        BindableProperty.Create(nameof(BottomLineColor), typeof(Color), typeof(CustomEntry), Color.Blue);

    public Color BottomLineColor
    {
        get => (Color) GetValue(BottomLineColorProperty);
        set => SetValue(BottomLineColorProperty, value);
    }
}

CustomEntryRenderer

public class CustomEntryRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<CustomEntry, TextInputLayout>
{
    public CustomEntryRenderer(Context context) : base(context)
    {
    }

    protected EditText EditText => Control.EditText;

    protected override TextInputLayout CreateNativeControl()
    {
        var textInputLayout = new TextInputLayout(Context);
        var editText = new AppCompatEditText(Context);
        editText.SetTextSize(ComplexUnitType.Sp, (float) Element.FontSize);
        textInputLayout.AddView(editText);
        return textInputLayout;
    }

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

        if (e.NewElement != null)
        {
            var ctrl = CreateNativeControl();
            SetNativeControl(ctrl);

            var activeColor = Element.BottomLineColor.ToAndroid(global::Android.Resource.Attribute.ColorAccent, Context);

            var hintText = Control.Class.GetDeclaredField("mFocusedTextColor");
            hintText.Accessible = true;
            hintText.Set(Control, new ColorStateList(new int[][] {new[] {0}}, new int[] {activeColor}));

            EditText.Enabled = Element.IsEnabled;
            Control.Hint = Element.Placeholder;
            EditText.SetTextColor(Element.TextColor.ToAndroid());
        }
    }
}

Element.TextColor & activeColor 正确获取我在参数中传递的颜色。

但这是我的结果:

【问题讨论】:

    标签: c# android xamarin.forms editor


    【解决方案1】:

    Xamarin Forms and Material design,如何改变提示颜色

    您可以将AppCompatEditText 添加到TextInputLayout

    protected EditText EditText => Control.EditText;
    
    protected override TextInputLayout CreateNativeControl()
    {
        var textInputLayout = new TextInputLayout(Context);
        var editText = new AppCompatEditText(Context);
        editText.SetTextSize(ComplexUnitType.Sp, (float)Element.FontSize);
        textInputLayout.AddView(editText);
        return textInputLayout;
    }
    

    然后像这样设置提示文本颜色:

    var activeColor = Element.ActiveHintTextColor.ToAndroid(global::Android.Resource.Attribute.ColorAccent, Context);
    var hintText = Control.Class.GetDeclaredField("mFocusedTextColor");
    hintText.Accessible = true;
    hintText.Set(Control, new ColorStateList(new int[][] { new[] { 0 } }, new int[] { activeColor }));
    

    源代码

    自定义条目:

    public class CustomEntry : Entry
    {
        public static readonly BindableProperty ActiveHintTextColorProperty = BindableProperty.Create(nameof(ActiveHintTextColor),
           typeof(Color),
           typeof(CustomEntry),
           Color.Accent);
    
        /// <summary>
        /// ActiveHintTextColor summary. This is a bindable property.
        /// </summary>
        public Color ActiveHintTextColor
        {
            get { return (Color)GetValue(ActiveHintTextColorProperty); }
            set { SetValue(ActiveHintTextColorProperty, value); }
        }
    }
    

    渲染器:

    public class CustomEntryRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<CustomEntry, TextInputLayout>
    {
        public CustomEntryRenderer (Context context) : base(context)
        {
        }
    
        protected EditText EditText => Control.EditText;
    
        protected override TextInputLayout CreateNativeControl()
        {
            var textInputLayout = new TextInputLayout(Context);
            var editText = new AppCompatEditText(Context);
            editText.SetTextSize(ComplexUnitType.Sp, (float)Element.FontSize);
            textInputLayout.AddView(editText);
            return textInputLayout;
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<XfxEntry> e)
        {
            base.OnElementChanged(e);
    
            if (e.NewElement != null)
            {
                var ctrl = CreateNativeControl();
                SetNativeControl(ctrl);
    
                var activeColor = Element.ActiveHintTextColor.ToAndroid(global::Android.Resource.Attribute.ColorAccent, Context);
    
                var hintText = Control.Class.GetDeclaredField("mFocusedTextColor");
                hintText.Accessible = true;
                hintText.Set(Control, new ColorStateList(new int[][] { new[] { 0 } }, new int[] { activeColor }));
    
                EditText.Enabled = Element.IsEnabled;
                Control.Hint = Element.Placeholder;
                EditText.SetTextColor(Element.TextColor.ToAndroid());
            }
        }
    }
    

    用法:

    <local:CustomEntry
        Placeholder="Enter your name:"
        Text="Name"
        ActiveHintTextColor="Blue"
        PlaceholderColor="Black"
            />
    

    Effect

    更新:

    设置底线颜色:

    var element = (ITintableBackgroundView)EditText;
    //using AColor = Android.Graphics.Color;
    element.SupportBackgroundTintList = ColorStateList.ValueOf(AColor.White);
    

    【讨论】:

    • 你好@YorkShen,谢谢你的回答,但它对我不起作用。底线颜色没有改变。我已经用我的代码编辑了我的线程
    • @Naografix,你需要什么底色?
    • 我的页面中的白色。白色的 PlaceHolderColor、TextColor 和 BottomLine
    • @Naografix,快乐的编码。 :)
    【解决方案2】:

    我知道这不是最好的解决方案,但它有时会很有用:

    • 打开:Android/Resources/values/colors.xml
    • colorAccent 更改为所需颜色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多