【问题标题】:Xamarin.Forms Entry change cursor colorXamarin.Forms 条目更改光标颜色
【发布时间】:2020-02-22 12:28:29
【问题描述】:

我尝试更改 Xamarin 表单条目上的光标颜色。到目前为止,我遵循了这个论坛帖子的解决方案:

https://forums.xamarin.com/discussion/138361/change-cursor-color-in-entry

自定义渲染器中的这段代码是什么:

IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
IntPtr mCursorDrawableResProperty = JNIEnv.GetFieldID(IntPtrtextViewClass, "mCursorDrawableRes", "I");

// my_cursor is the xml file name which we defined above
JNIEnv.SetField(Control.Handle, mCursorDrawableResProperty, Resource.Drawable.my_cursor);

不幸的是,这在我的 Android Q 模拟器/设备上不再起作用了。我得到了这个例外:

Java.Lang.NoSuchFieldError: no "I" field "mCursorDrawableRes" in class "Landroid/widget/TextView;"

还有其他方法吗?

示例:https://1drv.ms/u/s!Ang3D30bKDOhqPATE80z8n3pUX9JxQ?e=L08oiB

【问题讨论】:

  • 在您提到的帖子中,回答问题的人还分享了一个示例应用程序。我在我身边运行它,光标使用我在 colors.xml 文件中设置的颜色正确显示。你试过了吗?
  • 是的,我也试过了。但它没有用。我认为 Xamarin.Forms.visual.Material 有一些东西会覆盖它。
  • 正如我所说,我下载了示例并按原样运行。有效。然后我更新了 Xamarin.Forms 版本并再次运行该项目,它运行没有问题。我在颜色文件中做了一些更改,它们反映在构建的应用程序中。然后,您的问题可能在其他地方……在您的来源中?当您按原样运行示例时,您仍然会出错吗?和你发布的一样吗?
  • 据我所知,该示例不使用 Xamarin.Forms.Visual.Material,这对颜色有影响。因此,我在接受的答案中使用了使用反射的解决方案。
  • 使用前面提到的示例,我在项目中安装了 Visual.Material,代码仍然按需要运行。也许您可以详细说明您的问题,并让我们知道能够重现您的问题的确切步骤:)

标签: xamarin.forms


【解决方案1】:

如果有人在使用 android SDK 10 (Q) 编译项目时遇到崩溃,请这样做:

    if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
    {
        Control.SetTextCursorDrawable(0); //This API Intrduced in android 10
    }
    else
    {
        IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
        IntPtr mCursorDrawableResProperty = JNIEnv.GetFieldID(IntPtrtextViewClass, "mCursorDrawableRes", "I");
        JNIEnv.SetField(Control.Handle, mCursorDrawableResProperty, 0);
    }

干杯!

【讨论】:

  • 如何在 Xamarin.Forms 的共享项目中做同样的事情?
  • @sham 你可以编写自定义条目渲染器,你可以在那里编写这个。代码示例可见Here
  • 如果渲染器是“MaterialEntryRenderer”类型,这仍然会失败
【解决方案2】:

当你使用EntryRenderer时,ControlEntry的类型:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{

    base.OnElementChanged(e);
    Control.SetTextCursorDrawable(Resource.Drawable.my_cursor);

}

当你使用MaterialEntryRenderer时,ControlMaterialFormsTextInputLayout的类型,所以当你更改MaterialFormsTextInputLayoutmCursorDrawableRes时它不起作用,甚至不能被发现所以你得到异常,正确的方法是:

public class EntryRendererForAndroid : MaterialEntryRenderer
{
    public EntryRendererForAndroid(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {

        base.OnElementChanged(e);

        Control.EditText.SetTextCursorDrawable(Resource.Drawable.my_cursor);

    }
}

【讨论】:

  • 看起来很棒!但是当我在示例中使用它时,出现以下异常: Java.Lang.NoSuchMethodError: 'no non-static method "Landroid/support/design/widget/TextInputEditText;.setTextCursorDrawable(I)V “'我做错了吗?
  • @NPadrutt 您可以查看我的示例here。顺便说一句,如果您使用 MaterialEntryRenderer,该条目将成为 MaterialFormsTextInputLayout,它可能看起来不像 Xamarin.forms 中的本机条目,这是设计使然。
  • 感谢您的样品。不幸的是,这在我的计算机上崩溃并出现同样的错误。我将在另一台计算机上检查我是否有某种损坏的安装。
  • AAH,问题是,我在 Android 9 模拟器上进行了测试。此 API 仅适用于 Android 10 及更高版本。所以我不得不添加一个版本检查。 :)
  • @NPadrutt 如何以及在何处为共享项目添加版本检查?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多