You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Android深色主题下.NET MAUI禁用Entry的文本颜色?

解决Android深色主题下禁用Entry文本可读性问题

在Android 14及更早版本的深色主题中,禁用状态的Entry控件文本默认灰色与黑色背景对比度不足,由于Xamarin.Forms/.NET MAUI未直接暴露禁用状态的文本颜色属性,可通过平台级编程方式修改,以下是两种主流框架的解决方案:

.NET MAUI 方案:自定义Entry Handler

在Android平台项目中,通过修改Entry的Handler来设置禁用状态的文本颜色,可区分深浅主题适配:

// 在Platforms/Android/Program.cs中添加,或创建单独的Handler配置类
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CustomDisabledTextColor", (handler, view) =>
{
    if (view is Entry entry)
    {
        var currentTheme = Android.App.Application.Context.Resources.Configuration.UiMode & Android.Content.Res.UiMode.NightMask;
        Android.Graphics.Color textColor;

        if (!entry.IsEnabled)
        {
            // 深色主题下设置高对比度颜色
            textColor = currentTheme == Android.Content.Res.UiMode.NightYes 
                ? Android.Graphics.Color.ParseColor("#E0E0E0") 
                : Android.Graphics.Color.ParseColor("#8A000000");
            handler.PlatformView.SetTextColor(textColor);
        }
        else
        {
            // 启用状态恢复绑定的TextColor
            textColor = entry.TextColor.ToAndroid();
            handler.PlatformView.SetTextColor(textColor);
        }
    }
});

Xamarin.Forms 方案:自定义Entry渲染器

创建Android平台的自定义渲染器,监听IsEnabled属性变化来切换文本颜色:

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace YourAppNamespace.Droid.Renderers
{
    public class CustomEntryRenderer : EntryRenderer
    {
        public CustomEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                UpdateDisabledTextColor();
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == nameof(Entry.IsEnabled))
            {
                UpdateDisabledTextColor();
            }
        }

        private void UpdateDisabledTextColor()
        {
            var currentTheme = Context.Resources.Configuration.UiMode & Android.Content.Res.UiMode.NightMask;
            Android.Graphics.Color targetColor;

            if (!Element.IsEnabled)
            {
                targetColor = currentTheme == Android.Content.Res.UiMode.NightYes
                    ? Android.Graphics.Color.ParseColor("#E0E0E0")
                    : Android.Graphics.Color.ParseColor("#8A000000");
            }
            else
            {
                targetColor = Element.TextColor.ToAndroid();
            }

            Control.SetTextColor(targetColor);
        }
    }
}

补充说明

  • 直接修改TextColor属性无效的原因:禁用状态下,Android系统会覆盖控件的文本颜色为默认禁用色,而非使用TextColor的衍生色。
  • AppThemeBinding无法直接生效:Entry控件未提供绑定禁用状态文本色的专属属性,因此必须通过平台级代码干预。

内容的提问来源于stack exchange,提问作者Doug_M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:23:22