【问题标题】:Change EditText Cursor Color in Xamarin Forms在 Xamarin 表单中更改 EditText 光标颜色
【发布时间】:2014-12-05 12:37:01
【问题描述】:

我的ContentPage 中有一个Entry,并且正在Xamarin Android 中进行渲染。 这里我的问题是EditText 背景颜色是白色,光标颜色也是白色。 在这里,我想将光标颜色更改为黑色。 有没有办法改变光标颜色? 这是我的代码。

ExtendedEntry 的入口:

public class ExtendedEntry : Entry { }

在内容页面中使用扩展条目:

var txtPhoneNumber = new ExtendedEntry { Placeholder = "Phone Number", Keyboard = Keyboard.Numeric, TextColor = Color.Black };

在 Xamarin Android 中渲染 ExtendedEntry:

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRender))]
namespace Project.Droid
{
    public class ExtendedEntryRender : EntryRenderer
    {
        // Override the OnElementChanged method so we can tweak this renderer post-initial setup
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {   // perform initial setup
                // lets get a reference to the native control
                var nativeEditText = (global::Android.Widget.EditText)Control;
                // do whatever you want to the textField here!
            nativeEditText.SetBackgroundResource(Resource.Drawable.text_box);
            }
        }
    }
}

谁能帮我解决这个问题? 提前致谢。

【问题讨论】:

    标签: xamarin xamarin.android xamarin.forms


    【解决方案1】:

    您可以使用以下方法更改光标颜色:

    IntPtr IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
    IntPtr mCursorDrawableResProperty = JNIEnv.GetFieldID (IntPtrtextViewClass, "mCursorDrawableRes", "I");
    JNIEnv.SetField (Control.Handle, mCursorDrawableResProperty, 0); // replace 0 with a Resource.Drawable.my_cursor 
    

    0 将保持与您条目上的 TextColor 相同的颜色。

    【讨论】:

    • 您可以将这里指出的代码与平台效果结合起来,避免创建派生类
    【解决方案2】:

    编辑:要更改光标颜色,唯一的选择是通过添加示例来更改主题:

    主题 = MainActivity 中的“android:style/Theme.Holo.light”

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2012-07-16
      • 2016-05-22
      • 2016-02-29
      • 2022-01-27
      相关资源
      最近更新 更多