【问题标题】:Pass fields with enter key?用回车键传递字段?
【发布时间】:2014-02-18 16:53:22
【问题描述】:

我正在寻找一种在 VerticalLayout 或其他中使用 enter 键传递字段的方法。在 vaadin 书中,有一个带有 Shortcut 和 Handler 侦听器的示例,但我不知道如何实现它。

我正在尝试这个。

public class MyWindow extends Window implements Handler{
private Action action_enter; //pass fields with enter
private Action action_esc;
private TextField name, lastName;

public MyWindow(){
    super("this window is opened");
    VerticalLayout vLayout = new VerticalLayout();
    setContent(vLayout);

    center();
    setModal(true);
    setClosable(false);
    setDraggable(false);
    setResizable(false);    

    //actions
    action_enter = new ShortcutAction("Enter key", ShortcutAction.KeyCode.ENTER, null);
    action_esc = new ShortcutAction("Esc key", ShortcutAction.KeyCode.ESCAPE, null);
    addActionHandler(this);

    //fields
    name = new TextField("Name");
    lastName = new TextField("Last name");

    name.focus();
    vLayout.addComponent(name);
    vLayout.addComponent(lastName);     
}


@Override
public Action[] getActions(Object target, Object sender) {      
    return new Action[] { action_enter, action_esc };
}

@Override
public void handleAction(Action action, Object sender, Object target) {

    /** close window with esc key */
    if(action == action_esc){
        close();
    }   

    /** pass fields with enter key */
    if(action == action_enter){
        //here pass fields with enter key 
    }
}

}

有什么想法吗?

【问题讨论】:

  • 如何使用 Enter 代替 Tab 更改 TextField 的焦点?

标签: java frameworks vaadin vaadin7


【解决方案1】:

用 ShortcutListener 试试这个方法:

    ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

        @Override
        public void handleAction(Object sender, Object target) {

            if (target instanceof VerticalLayout) { // VerticalLayout or other
                 // sending fileds here
            }
        }
    };

    addShortcutListener(skEnterListener);

使用 Enter 代替 Tab 更改 TextField 的焦点:

    final TextField tf1 = new TextField("tf1");
    tf1.setId("tf1");

    final TextField tf2 = new TextField("tf2");
    tf2.setId("tf2");


    ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

        @Override
        public void handleAction(Object sender, Object target) {

             if (target instanceof TextField) {

                 TextField field = (TextField) target;

                 if ("tf1".equals(field.getId())) {
                     tf2.focus();
                 }

                 if ("tf2".equals(field.getId())) {
                     tf1.focus();
                 }
             }
        }
    };

    addShortcutListener(skEnterListener);

【讨论】:

    【解决方案2】:

    没有提供访问器的接口可以让您找出当前聚焦的组件。可以通过com.vaadin.event.FieldEvents.FocusListenercom.vaadin.event.FieldEvents.BlurListener 接口获取部分(但不是全部)字段组件的焦点信息。

    您可以为所有可能的字段添加FocusListener 并记住每次调用它时,变量中的当前字段。 (问题:并非所有字段都提供 FocusListener。)然后当按下 ENTER 时,根据当前必须聚焦的聚焦字段(记住变量)聚焦下一个组件(借助简单的 List、LinkedList、Map、switch -case 等等)。为了让它更好地添加BlurListener 以及知道什么时候不关注下一个字段。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-28
      • 2015-07-28
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多