简体   繁体   中英

Android- No action when user presses enter

I designed an app in which user enters an input to an EditText view and presses enter to see some result. In some Android phones it shows the desired result but in some of them no action happens. I used this code:

public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_ENTER:
                        // Here is a function call
                        return true;
                    default:
                        break;
                }
            }

I could not figure out why it does not work on some phones.

Try this:

  public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Here is a function call
          return true;
        }
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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