按下“Enter”键时移动到下一个文本框而非提交(Windows Phone)

6
我有一个简单的表单,当用户在手机键盘上按下回车键时,我希望光标能移动到下一个文本框。这在通用Windows应用程序中可以实现吗?在Android中,键盘会显示“下一个/完成”按钮以在表单元素之间导航。

enter image description here

2个回答

7
你可以使用FocusManager来编程地移动焦点。
使用TextBox容器的KeyDown事件,例如一个StackPanel,来监听你的键盘事件。你的代码应该像这样工作。
    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"/>? - Bruno Medina
那么只有“输入”按钮,没有“完成/下一步”按钮吗? - testing

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接