【问题标题】:EditText cursor and pointer not changing colour properlyEditText 光标和指针没有正确改变颜色
【发布时间】:2017-12-26 07:15:46
【问题描述】:

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

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

2 次观看

  • EditTextA
  • EditTextB

4 种样式

  • StyleRRed 作为 colorAccent
  • StyleGGreencolorAccent
  • StyleBBluecolorAccent
  • StyleYYellow 作为 colorAccent

如果我将StyleR 作为当前样式并点击EditTextA,光标将立即显示为红色。如果我将样式更改为StyleG,点击EditTextA,输入一些内容并选择它,我将有一个红色光标,其下方有一个绿色指针。同时,如果我点击EditTextB,光标将变为绿色

我已尝试在 RunOnUiThread 中对 Invalidate()PostInvalidate() 两个视图进行处理,但它们不会更正它们的颜色。

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

【问题讨论】:

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


    【解决方案1】:

    基于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 ) {
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      我只需要用于 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)
              {
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2016-02-29
        • 2017-05-05
        • 2016-06-05
        • 2012-07-23
        • 2023-01-16
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        相关资源
        最近更新 更多