我有一个带有大量文本框的Windows 8商店应用程序。当我按下键盘上的Enter键时,我希望将focues移动到下一个控件。
我该怎么做?
由于
答案 0 :(得分:5)
您可以处理TextBoxes上的KeyDown / KeyUp事件(取决于您是否要在按键的开头或结尾处转到下一个事件。)
示例XAML:
<TextBox KeyUp="TextBox_KeyUp" />
代码背后:
private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
TextBox tbSender = (TextBox)sender;
if (e.Key == Windows.System.VirtualKey.Enter)
{
// Get the next TextBox and focus it.
DependencyObject nextSibling = GetNextSiblingInVisualTree(tbSender);
if (nextSibling is Control)
{
// Transfer "keyboard" focus to the target element.
((Control)nextSibling).Focus(FocusState.Keyboard);
}
}
}
完整的示例代码,包括GetNextSiblingInVisualTree()辅助方法的代码: https://github.com/finnigantime/Samples/tree/master/examples/Win8Xaml/TextBox_EnterMovesFocusToNextControl
请注意,使用FocusState.Keyboard调用Focus()会在其控件模板(例如Button)中显示具有此类rect的元素周围的虚线焦点rect。使用FocusState.Pointer调用Focus()不会显示焦点rect(您正在使用触摸/鼠标,因此您知道要与哪个元素进行交互)。
答案 1 :(得分:1)
我对&#34; GetNextSiblingInVisualTree&#34;进行了一些改进。功能。此版本搜索下一个TextBox而不是下一个对象。
import java.awt.*;
import java.awt.datatransfer.Transferable;
import javax.swing.*;
import java.awt.event.InputEvent;
public class DnDTransferableTest {
public static void main(String[] args) {
new DnDTransferableTest();
}
public DnDTransferableTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label1;
private JLabel label2;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
label1 = new JLabel("Drag Me.");
add(label1, gbc);
label2 = new JLabel("Drag Me too.");
gbc.gridx++;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
add(label2, gbc);
setTransferHandler(new TransferHandler()
{
@Override
public Transferable createTransferable(final JComponent c)
{
System.out.println("Inside Panel : createTransferable");
return null;
}
@Override
public int getSourceActions(final JComponent c)
{
System.out.println("Inside Panel : getSourceActions()");
return COPY;
}
@Override
public void exportAsDrag(final JComponent comp, final InputEvent e, final int action)
{
System.out.println("Inside Panel : getSourceActions()");
super.exportAsDrag(comp, e, action);
}
});
label1.setTransferHandler(new TransferHandler()
{
@Override
public Transferable createTransferable(final JComponent c)
{
System.out.println("Inside Label : createTransferable");
return null;
}
@Override
public int getSourceActions(final JComponent c)
{
System.out.println("Inside Label : getSourceActions()");
return COPY;
}
@Override
public void exportAsDrag(final JComponent comp, final InputEvent e, final int action)
{
System.out.println("Inside Label : getSourceActions()");
super.exportAsDrag(comp, e, action);
}
});
}
}
}