How to consistently set EditText Selected Underline Color Programmatically

前端 未结 5 2071
情深已故
情深已故 2021-02-20 09:05

I\'m trying to build a renderer for Xamarin Forms. The renderer needs to set the EditText underline color to \"Active Color\" when selected and \"Hint Color\" when

5条回答
  •  抹茶落季
    2021-02-20 09:56

    So the solution for me was to go pure AppCompat

    So I'm adding an AppCompatEditText to the TextInputLayout

    protected EditText EditText => Control.EditText;
    
    protected override TextInputLayout CreateNativeControl()
    {
        var textInputLayout = new TextInputLayout(Context);
        var editText = new AppCompatEditText(Context)
        {
            SupportBackgroundTintList = ColorStateList.ValueOf(GetPlaceholderColor())
        };
        textInputLayout.AddView(editText);
        return textInputLayout;
    }
    

    Then from there I was able to set the underline consistently with this.

    private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
    {
        _hasFocus = args.HasFocus;
        SetUnderlineColor(_hasFocus ?  GetActivePlaceholderColor(): GetPlaceholderColor());
    } 
    
    private void SetUnderlineColor(AColor color)
    {
        var element = (ITintableBackgroundView)EditText;
        element.SupportBackgroundTintList = ColorStateList.ValueOf(color);
    }
    

    full source code here.

提交回复
热议问题