【发布时间】:2016-01-20 09:47:46
【问题描述】:
我有一个简单的表单,我希望当用户在他的手机键盘上按下回车键时,光标会移动到下一个文本框。 这可以在通用 Windows 应用程序中完成吗? 在 android 中,键盘显示一个 Next/Done 键以在表单元素中导航。
【问题讨论】:
我有一个简单的表单,我希望当用户在他的手机键盘上按下回车键时,光标会移动到下一个文本框。 这可以在通用 Windows 应用程序中完成吗? 在 android 中,键盘显示一个 Next/Done 键以在表单元素中导航。
【问题讨论】:
您可以使用 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
【讨论】:
<TextBox KeyDown="TextBox_KeyDown"/>
你有类似yourTextBoxName.Focus() 的东西吗?还为 New Password 文本框使用 KeyDownEvent 并检查以下内容
if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A)
{
confirmPassword.Focus();//change confirmPassword to your controls actual name
}
【讨论】: