【问题标题】:Change focus color of Entry control in Xamarin Forms在 Xamarin Forms 中更改条目控件的焦点颜色
【发布时间】:2017-09-19 12:59:18
【问题描述】:

如何在 Xamarin 表单的 Entry 控件中更改焦点边框和光标颜色?在模拟器里是标准的红色?

我在我的 Android 项目中添加了这个

[assembly: ExportRenderer(typeof(CustomEntryControl), typeof(MyEntryRenderer))]
namespace MyApp.Droid
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
            }
        }
    }
}

但找不到边框或光标的属性?

【问题讨论】:

    标签: c# android xaml xamarin.forms custom-controls


    【解决方案1】:

    您可以在 style.xml 文件中更改 Android 项目中的条目焦点颜色。路径为:Resources/values/styles.xml

    然后,查看“colorAccent”属性来设置自定义颜色。

    【讨论】:

      【解决方案2】:

      试试下面的代码

        public class EntryCustomRenderer : EntryRenderer
          {
              protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
          {
              base.OnElementChanged(e);
      
              if (Control != null)
              {
                  Control.SetBackgroundColor(global::Android.Graphics.Color.Transparent);
      
                  // 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); 
              }
          }
      }
      

      请注意,如果您为条目设置了 TextColor,如果您为资源 id 保留“0”值,则光标将使用该颜色

      【讨论】:

      • 啊,但我使用的是 Xamarin Forms,而不是原生 Android
      • 好的 以后试试这个!
      • 希望对你有帮助
      【解决方案3】:

      你好,试试这个代码来改变 iOS 入口看起来像原生 Android 入口 在你的 ios 部分添加这个渲染器;焦点和非焦点颜色也在变化

      记住这一行来添加上面的命名空间 [程序集:ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]

      public class CustomEntryRenderer : EntryRenderer
      {
          private CALayer _borderLayer;
          protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
          {
              base.OnElementChanged(e);
      
              if (Control == null)
                  return;
      
              Control.BorderStyle = UITextBorderStyle.None;
      
              var element = Element as Entry;
              if (element == null)
                  return;
      
              //DrawBorder(element.BorderColor.ToCGColor());
              DrawBorder(UIColor.FromRGB(156, 156, 156));
      
              e.NewElement.Unfocused += (sender, evt) =>
              {
                  DrawBorder(UIColor.FromRGB(156, 156, 156)); // unfocused, set color
      
              };
              e.NewElement.Focused += (sender, evt) =>
              {
                  DrawBorder(UIColor.FromRGB(245, 0, 47)); ; // focused, set color
              };
          }
          protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
          {
              base.OnElementPropertyChanged(sender, e);
      
              var element = Element as Entry;
              if (element == null)
                  return;
      
              DrawBorder(UIColor.FromRGB(156, 156, 156));
          }
          public override CGRect Frame
          {
              get { return base.Frame; }
              set
              {
                  base.Frame = value;
      
                  var element = Element as Entry;
                  if (element == null)
                      return;
      
                  // DrawBorder(element.BorderColor.ToCGColor());
                  DrawBorder(UIColor.FromRGB(156, 156, 156));
              }
          }
          private void DrawBorder(UIColor borderColor)
          {
              if (Frame.Height <= 0 || Frame.Width <= 0)
                  return;
      
              if (_borderLayer == null)
              {
                  _borderLayer = new CALayer
                  {
                      MasksToBounds = true,
                      Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f),
                      BorderColor = borderColor.CGColor,
                      BorderWidth = 1.0f
                  };
      
                  Control.Layer.AddSublayer(_borderLayer);
                  Control.BorderStyle = UITextBorderStyle.None;
              }
              else
              {
                  _borderLayer.BorderColor = borderColor.CGColor;
                  _borderLayer.Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-26
        • 2019-07-29
        • 2022-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-22
        相关资源
        最近更新 更多