如何以编程方式一致地设置EditText选定的下划线颜色

Cha*_*ell 11 android xamarin.android textinputlayout

我正在尝试为Xamarin Forms构建渲染器.渲染器需要EditText在选中时将下划线颜色设置为"活动颜色",并在取消选择时将其设置为"提示颜色".我的初始设置看起来像这样.

注意:这里的路径完整的源文件
https://github.com/XamFormsExtended/Xfx.Controls/blob/issue-%236/src/Xfx.Controls.Droid/Renderers/XfxEntryRendererDroid.cs

// called when control is created or when Colors are changed.
protected virtual void SetLabelAndUnderlineColor()
{
    var defaultColor = GetPlaceholderColor();
    var activeColor = GetActivePlaceholderColor();

    SetHintLabelDefaultColor(defaultColor);
    SetHintLabelActiveColor(activeColor);
    SetUnderlineColor(_hasFocus ? activeColor : defaultColor);
}

private void SetUnderlineColor(AColor color)
{
    var bg = ColorStateList.ValueOf(color);
    ViewCompat.SetBackgroundTintList(EditText,bg);
}

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

private void SetHintLabelDefaultColor(AColor color)
{
    var hint = Control.Class.GetDeclaredField("mDefaultTextColor");
    hint.Accessible = true;
    hint.Set(Control, new ColorStateList(new int[][] { new[] { 0 } }, new int[] { color }));
}
Run Code Online (Sandbox Code Playgroud)

除此之外,我还有一个OnClickListener,只有在状态发生变化时才更新下划线

private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
    _hasFocus = args.HasFocus;
    SetUnderlineColor(args.HasFocus ? GetPlaceholderColor() : GetActivePlaceholderColor());
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我点击EditText时,它会打击或错过我将要看到的下划线颜色.我几乎可以保证这是android:colorAccent第一次默认.然后在"提示颜色"和"占位符颜色"之间切换.

注意:如果我将SetUnderlineColor方法更改为此(下面),它不再使用混合中的"提示颜色",但我仍然将android:colorAccent颜色作为初始下划线颜色,之后它的行为与预期一致.

private void SetUnderlineColor(AColor color)
{
    var bg = EditText.Background;
    DrawableCompat.SetTint(bg,color);
    EditText.SetBackground(bg);
}
Run Code Online (Sandbox Code Playgroud)

如何将EditText的INITIAL选定颜色设置为我选择的activeColor"聚焦颜色"(紫色)?

在这个动画中,我只是选择并取消选择EditText 在此输入图像描述

Cha*_*ell 9

因此,我的解决方案是使用纯AppCompat

所以我加入AppCompatEditTextTextInputLayout

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;
}
Run Code Online (Sandbox Code Playgroud)

然后从那里我能够与此一致地设置下划线.

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);
}
Run Code Online (Sandbox Code Playgroud)

这里有完整的源代码.