我尝试更改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
使用时EntryRenderer
,控件的类型为Entry
:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
Control.SetTextCursorDrawable(Resource.Drawable.my_cursor);
}
当您使用的同时MaterialEntryRenderer
,该控制是类型MaterialFormsTextInputLayout
,所以它不会工作,当你改变mCursorDrawableRes
的MaterialFormsTextInputLayout
,它甚至无法找到,所以你得到的异常,正确的做法是:
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句