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

MAUI Android平台HorizontalScrollView内Entry滑动时意外获焦问题求助

MAUI Android平台HorizontalScrollView内Entry滑动时意外获焦问题求助

大家好,我在MAUI应用中通过Handler映射集成了原生Android的HorizontalScrollView,目前碰到了一个棘手的问题:只要我在这个HorizontalScrollView的任意区域(比如屏幕中间位置)滑动,内部嵌套的Entry控件就会意外自动获取焦点,这完全不是我想要的效果。

环境与问题详情

  • 运行平台:Android
  • 具体现象:在HorizontalScrollView范围内的任何位置执行滑动操作(并非直接点击Entry),Entry都会自动获得焦点。

以下是我简化后的复现代码:

public partial class MainPage : ContentPage 
{
    Entry mauiEntry;

    public MainPage() 
    {
        InitializeComponent();

        // Initialize the Entry
        mauiEntry = new Entry 
        {
            Placeholder = "Type here",
            BackgroundColor = Colors.LightGray,
            TextColor = Colors.Black,
            HeightRequest = 40,
            WidthRequest = 200
        };

        // Create the innermost view that will contain the Entry
        var innerView = new ContentView 
        {
            Content = mauiEntry,
            BackgroundColor = Colors.Beige,
            Padding = new Thickness(10)
        };

        // Create the middle view that contains the inner view
        var middleView = new ContentView 
        {
            Content = innerView,
            BackgroundColor = Colors.Yellow,
            WidthRequest = 300,
            HeightRequest = 500,
            Padding = new Thickness(20)
        };

        // Create the top-level grid that contains the middle view
        var container = new Grid 
        {
            WidthRequest = 350,
            HeightRequest = 550,
            BackgroundColor = Colors.Violet
        };

        // Add the middle view to the grid
        container.Children.Add(middleView);

        var horizontalScrollWrapper = new InteractiveScrollViewExt(container) 
        {
            BackgroundColor = Colors.LightPink
        };
        myGrid.Children.Add(horizontalScrollWrapper);
    }

    private void Unfocu_Clicked(object sender, EventArgs e) 
    {
        mauiEntry.Unfocus();
    }
}

public class InteractiveScrollViewExt : View 
{
    public View Content { get; }

    public InteractiveScrollViewExt(View child) => Content = child;
}

public class InteractiveScrollViewExtHandler : ViewHandler<InteractiveScrollViewExt, HorizontalScrollView> 
{
    public InteractiveScrollViewExtHandler() : base(ViewHandler.ViewMapper) 
    {
    }

    protected override HorizontalScrollView CreatePlatformView() 
    {
        var context = MauiApplication.Current.ApplicationContext!;
        var scrollView = new HorizontalScrollView(context);

        if (VirtualView.Content is not null) 
        {
            var viewHandler = VirtualView.Content.ToHandler(MauiContext!);
            var platformView = viewHandler.PlatformView as Android.Views.View;
            scrollView.AddView(platformView);
        }

        return scrollView;
    }
}

public class HorizontalScrollViewer : HorizontalScrollView 
{
    public HorizontalScrollViewer(Context? context) : base(context) 
    {
        this.ClipToOutline = true;
    }
}

预期行为

Entry控件应该仅在被直接点击时才获取焦点,在HorizontalScrollView内滑动或点击其他区域时,都不应该改变它的焦点状态。

希望有遇到过类似问题或者熟悉MAUI Handler与原生控件交互的朋友能帮我分析下原因,或者提供解决思路,非常感谢!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 12:58:06