简体   繁体   English

GWT中的ENTER处理程序

[英]ENTER handler in GWT

I'm new in Google web toolkit. 我是Google网络工具包的新手。 I should implement a button "enter key" in my project. 我应该在我的项目中实现一个按钮“输入密钥”。 Can you please help me? 你能帮我么?

In the login menu there is a click handler, on which the user enters her login information and be able to press "enter" afterward. 在登录菜单中,有一个单击处理程序,用户可以在其上输入她的登录信息,然后可以按“输入”。

Code I have so far: 我到目前为止的代码:

Button btnLogin = new Button("Login");
    btnLogin.addListener(new ButtonListenerAdapter() {
        public void onClick(Button button, EventObject e) {
            Date date = new Date();
            Cookies.setCookie("user", username.getValueAsString(), date);
            Cookies.setCookie("pass", password.getValueAsString(), date);               
            Cookies.setCookie("save_login", checkbox.getValueAsString(), date);

            formPanel.getForm().submit("Login", null, Connection.POST, "Logging...", false);
        }           
    });

If you like to submit the data , when the user uses the enter key, just add a KeyUpHandler to your button: 如果您想提交数据,则当用户使用回车键时,只需将KeyUpHandler添加到您的按钮即可:

   button.addKeyUpHandler(new KeyUpHandler() {
      @Override
      public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
          // do submit form
        }
      }
    });

You should use Handler instead of listener. 您应该使用Handler而不是侦听器。 Listener are old school and starting with GWT 2.0 deprecated. 侦听器是旧式的,从GWT 2.0开始不推荐使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM