EditText光标和指针不能正确改变颜色

时间:2017-07-20 14:14:01

标签: android android-edittext xamarin.android android-styles

我有一个包含4种样式的应用程序,每种样式都有不同的colorAccent。根据用户操作,正在使用的样式可能会发生变化,colorAccent

也会发生变化

还有两个 somewhat-static EditText次观看(这些视图在应用的生命周期中不会消失,因为它们出现在“主页”页面中应用)。作为参考,我们假设我有:

2次观看

  • EditTextA
  • EditTextB

4种风格

  • StyleR RedcolorAccent
  • StyleG GreencolorAccent
  • StyleB BluecolorAccent
  • StyleY YellowcolorAccent

如果我将StyleR作为当前样式并点按EditTextA,则光标会立即显示为红色。如果我将样式更改为StyleG,请点按EditTextA,输入内容并选择它,我会有红色光标,其中绿色它下面的指针。同时,如果我点按EditTextB,光标将绿色

我在Invalidate()内尝试了PostInvalidate()RunOnUiThread两种观点,但他们不会更正其颜色。

在样式更改之间膨胀的任何其他EditText都会获得正确的颜色。

2 个答案:

答案 0 :(得分:0)

根据this answer by Jared Rummler,我设法在Xamarin中做了我想做的事。这是我最终得到的代码:

public static void SetCursorColor( this EditText editText, Resources resources, Int32 colorResourceId ) {
    try {
        TextView
            textViewTemplate = new TextView( editText.Context );

        //
        // EditText Cursor
        //
        var field = textViewTemplate.Class.GetDeclaredField( "mCursorDrawableRes" );
        field.Accessible = true;
        Int32 drawableResId = field.GetInt( editText );

        field = textViewTemplate.Class.GetDeclaredField( "mEditor" );
        field.Accessible = true;
        var editor = field.Get( editText );

        Drawable drawable = resources.GetDrawable( drawableResId );
        drawable.SetColorFilter( resources.GetColor( colorResourceId ), PorterDuff.Mode.SrcIn );
        Drawable[] drawables = { drawable, drawable };

        field = editor.Class.GetDeclaredField( "mCursorDrawable" );
        field.Accessible = true;
        field.Set( editor, drawables );

        //
        // EditText Pointer
        //
        String[]
            fieldsNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" },
            drawablesNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };

        for( Int32 index = 0; index < fieldsNames.Length && index < drawablesNames.Length; index++ ) {
            String
                fieldName = fieldsNames[ index ],
                drawableName = drawablesNames[ index ];

            field = textViewTemplate.Class.GetDeclaredField( fieldName );
            field.Accessible = true;
            Int32 handle = field.GetInt( editText );

            Drawable handleDrawable = resources.GetDrawable( handle );
            handleDrawable.SetColorFilter( resources.GetColor( colorResourceId ), PorterDuff.Mode.SrcIn );

            field = editor.Class.GetDeclaredField( drawableName );
            field.Accessible = true;
            field.Set( editor, handleDrawable );
        }
    } catch( Exception exception ) {

    }
}

答案 1 :(得分:0)

对于Android项目,我只需要白色光标和指针,并且基于“ auhmaan”答案,我在Droid Renderer中编写了此代码,并且可以正常工作。也许对某人会有帮助:

if (Control != null && Element != null)
{
    // set the cursor color the same as the entry TextColor
    IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
    IntPtr mCursorDrawableResProperty =
    JNIEnv.GetFieldID(IntPtrtextViewClass, "mCursorDrawableRes", "I");
    // replace 0 with a Resource.Drawable.my_cursor 
    JNIEnv.SetField(Control.Handle, mCursorDrawableResProperty, 0);

    try
        {
            TextView textViewTemplate = new TextView(Control.Context);

            var field = textViewTemplate.Class.GetDeclaredField("mEditor");
            field.Accessible = true;
            var editor = field.Get(Control);

            //
            // EditText Pointer
            //
            String[]
            fieldsNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" },
            drawablesNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };

            for (Int32 index = 0; index < fieldsNames.Length && index < drawablesNames.Length; index++)
                {
                    String
                    fieldName = fieldsNames[index],
                    drawableName = drawablesNames[index];

                    field = textViewTemplate.Class.GetDeclaredField(fieldName);
                    field.Accessible = true;
                    Int32 handle = field.GetInt(Control);

                    Drawable handleDrawable = Resources.GetDrawable(handle);

                  handleDrawable.SetColorFilter(Xamarin.Forms.Color.White.ToAndroid(), PorterDuff.Mode.SrcIn);

                    field = editor.Class.GetDeclaredField(drawableName);
                    field.Accessible = true;
                    field.Set(editor, handleDrawable);
                }
        }
        catch (Exception ex)
        {
        }
    }