【问题标题】:Move to next textbox when 'Enter' key is pressed instead of submit (Windows Phone)当按下“Enter”键而不是提交时移动到下一个文本框(Windows Phone)
【发布时间】:2016-01-20 09:47:46
【问题描述】:

我有一个简单的表单,我希望当用户在他的手机键盘上按下回车键时,光标会移动到下一个文本框。 这可以在通用 Windows 应用程序中完成吗? 在 android 中,键盘显示一个 Next/Done 键以在表单元素中导航。

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    您可以使用 FocusManager 以编程方式移动焦点。

    使用 TextBox 容器(比如 StackPanel)的 KeyDown 事件来监听您的键盘事件。所以你的代码会这样工作

        private void stackPanel_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (FocusManager.GetFocusedElement() == inputTextBox) // Change the inputTextBox to your TextBox name
                {
                    FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
                    FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
                }
                else
                { 
                    FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
                }
    
                // Make sure to set the Handled to true, otherwise the RoutedEvent might fire twice
                e.Handled = true;
            }
        }
    

    有关 FocusManager 的更多详细信息,请参阅https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.input.focusmanager.trymovefocus

    关于 KeyDown 的更多详细信息,请参阅https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.keydown

    【讨论】:

    • 唯一仍然困扰我的是,为什么将它分配给 StackPanel 而不是文本框,例如:<TextBox KeyDown="TextBox_KeyDown"/>
    • 所以只有进入按钮,没有完成/下一步按钮?
    【解决方案2】:

    你有类似yourTextBoxName.Focus() 的东西吗?还为 New Password 文本框使用 KeyDownEvent 并检查以下内容

    if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
    {
         confirmPassword.Focus();//change confirmPassword to your controls actual name    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 2017-11-22
      • 2014-04-04
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多