Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I detect the "enter" character when editing using a soft keyboard in an EditText on Android Jelly Bean?
    primarykey
    data
    text
    <p>I have an EditText, and am attempting to listen for keystrokes with setOnKeyListener, so that when I detect an Enter keypress, it performs some action. It works fine with hard keyboards and emulators, but not on my Galaxy Nexus. </p> <p>The documentation for <a href="http://developer.android.com/reference/android/view/View.html#setOnKeyListener%28android.view.View.OnKeyListener%29" rel="nofollow">setOnKeyListener</a> says "Key presses in software input methods will generally not trigger the methods of this listener", which explains why not.</p> <p>I don't want to explicitly add a "Done" button onscreen as in the <a href="http://developer.android.com/training/basics/firstapp/building-ui.html" rel="nofollow">Building Your First App</a> tutorial.</p> <p>I've now set it up to use the <code>android:inputType="text"</code> property in the layout xml, and it seems to have the desired effect. Is this considered the correct approach? Is it documented anywhere? Will it work on all devices? Is there a better way?</p> <p>Here is the code I'm using in my Activity:</p> <pre><code> myEditText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) { // Process the entered text here myEditText.setText(""); return true; } return false; } } </code></pre> <p>This is based on the ToDo List Example in Chapter 4 of <a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-4-Application-Development.productCd-1118102274.html" rel="nofollow">Professional Android 4 Application Development</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload