How to consistently set EditText Selected Underline Color Programmatically

前端 未结 5 2072
情深已故
情深已故 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:39

    To change color you can use below code

     editText.getBackground().mutate().setColorFilter(your_color), PorterDuff.Mode.SRC_ATOP);
    

    And to set different color underline for EditText view on focus change

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_purple), PorterDuff.Mode.SRC_ATOP);
                }else {
                    editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_dark), PorterDuff.Mode.SRC_ATOP);
                }
            }
        });
    
    0 讨论(0)
  • 2021-02-20 09:46

    Modify your code in XfxEntryRendererDroid ControlOnFocusChange method like this :

    private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
    {
        _hasFocus = args.HasFocus;
        if (_hasFocus)
        {
            ...   
    
            EditText.PostDelayed(() =>
                {
                    //Add the following code
                    SetUnderlineColor(GetActivePlaceholderColor());
                    EditText.RequestFocus();
                    manager.ShowSoftInput(EditText, 0);
                },
                0);//Change it to 0
        }
        ...
    }
    

    Effect.

    0 讨论(0)
  • 2021-02-20 09:46

    Why don't you change the tint colour at runtime using this (May be in your text changed event):

    ViewCompat.SetBackgroundTintList(_YourView , ColorStateList.ValueOf(Color.ParseColor(Resources.GetString(Resource.Color.blueLine))));
    

    Anyways Goodluck!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 10:01

    You need to set the backgroundTintList or supportBackgroundTintList on the EditText to an instance of ColorStateList

    ColorStateList colorStateList = ColorStateList.valueOf(color)
    editText.setSupportBackgroundTintList(colorStateList)
    

    OR

    I think that if you want to change a bottom line color, then you can change using this below line

    editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);
    

    And Application theame like this:-

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    
        <item name="colorControlNormal">@color/colorAccent</item>
        <item name="colorControlActivated">@color/colorAccent</item>
        <item name="colorControlHighlight">@color/colorAccent</item>
    
    </style>
    

    Please check this Example

    Hope this Link1 Link2 helps you.

    0 讨论(0)
提交回复
热议问题