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

Uno平台C#项目如何通过Tab/Next键隐藏安卓触控键盘并跳转焦点

解决方案

以下两种实现方式可按需选择:

方式一:单控件事件绑定(适合少量文本框场景)

Uno平台安卓端可通过KeyDown+Completed双事件覆盖Tab、回车、输入法Next键的捕获,同时处理键盘隐藏和焦点跳转逻辑:

  • 第一步:修改示例TextBox的XAML代码,绑定事件:
<TextBox x:Name="textboxNumber" Width="120" PlaceholderText="Num" Margin="5,2" 
         InputScope="Number" TextAlignment="Center"
         FontSize="28" FontWeight="Bold" MaxLength="3" HorizontalAlignment="Center"
         KeyDown="OnTextBoxKeyDown" Completed="OnTextBoxCompleted"/>
  • 第二步:编写后台C#逻辑:
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

private async void OnTextBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key is VirtualKey.Tab or VirtualKey.Enter)
    {
        e.Handled = true;
        await HandleNextAndHideKeyboard(sender as TextBox);
    }
}

private async void OnTextBoxCompleted(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    // 适配部分安卓设备输入法Next键不触发KeyDown的情况
    await HandleNextAndHideKeyboard(sender as TextBox);
}

private async System.Threading.Tasks.Task HandleNextAndHideKeyboard(TextBox currentTextBox)
{
    // 隐藏触控键盘
    currentTextBox.IsTabStop = false;
    await InputPane.GetForCurrentView().TryHideAsync();
    currentTextBox.IsTabStop = true;

    // 自动跳转到下一个可聚焦控件
    var nextControl = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Next) as Control;
    nextControl?.Focus(FocusState.Programmatic);
}

提示:如果定制安卓ROM下键盘隐藏不生效,可在TryHideAsync前临时设置currentTextBox.IsEnabled = false,调用完成后延迟10ms再恢复为true即可。

方式二:附加属性封装(适合多文本框批量复用场景)

如果页面内有大量文本框需要该能力,可把逻辑封装为附加属性,无需每个控件单独绑定事件:

public static class TextBoxAutoNextExtension
{
    public static readonly DependencyProperty EnableAutoNextProperty = DependencyProperty.RegisterAttached(
        "EnableAutoNext", typeof(bool), typeof(TextBoxAutoNextExtension), new PropertyMetadata(false, OnPropertyChanged));

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not TextBox textBox) return;
        if ((bool)e.NewValue)
        {
            textBox.KeyDown += TextBoxKeyDown;
            textBox.Completed += TextBoxCompleted;
        }
        else
        {
            textBox.KeyDown -= TextBoxKeyDown;
            textBox.Completed -= TextBoxCompleted;
        }
    }

    private static async void TextBoxCompleted(object sender, RoutedEventArgs e)
    {
        await ProcessNextLogic(sender as TextBox);
    }

    private static async void TextBoxKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key is VirtualKey.Tab or VirtualKey.Enter)
        {
            e.Handled = true;
            await ProcessNextLogic(sender as TextBox);
        }
    }

    private static async System.Threading.Tasks.Task ProcessNextLogic(TextBox textBox)
    {
        textBox.IsTabStop = false;
        await InputPane.GetForCurrentView().TryHideAsync();
        textBox.IsTabStop = true;
        (FocusManager.FindNextFocusableElement(FocusNavigationDirection.Next) as Control)?.Focus(FocusState.Programmatic);
    }

    public static void SetEnableAutoNext(DependencyObject element, bool value) => element.SetValue(EnableAutoNextProperty, value);
    public static bool GetEnableAutoNext(DependencyObject element) => (bool)element.GetValue(EnableAutoNextProperty);
}

使用时直接在XAML中添加属性即可:

<TextBox x:Name="textboxNumber" Width="120" PlaceholderText="Num" Margin="5,2" 
         InputScope="Number" TextAlignment="Center"
         FontSize="28" FontWeight="Bold" MaxLength="3" HorizontalAlignment="Center"
         local:TextBoxAutoNextExtension.EnableAutoNext="True"/>

异常排查

  • 焦点跳转失败:检查目标控件的IsEnabled、IsTabStop属性是否均为True
  • 键盘隐藏失败:确认调用TryHideAsync时当前TextBox处于聚焦状态,部分定制ROM可添加10~50ms的延迟再执行隐藏逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 16:06:05