studio programacion para herramientas fundamentos desarrollo con avanzado aplicaciones android keyboard android-softkeyboard

programacion - manual android studio avanzado



Use la tecla "ENTER" en el teclado en lugar de hacer clic en el botón (6)

Lo haces al configurar OnKeyListener en tu EditText .

Aquí hay una muestra de mi propio código. Tengo un EditText llamado addCourseText , que llamará a la función addCourseFromTextBox cuando se hace addCourseFromTextBox en la tecla enter o d-pad.

addCourseText = (EditText) findViewById(R.id.clEtAddCourse); addCourseText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_ENTER: addCourseFromTextBox(); return true; default: break; } } return false; } });

Hola, tengo un buscado EditText y Button búsqueda. Cuando escribo el texto buscado, me gustaría utilizar la tecla ENTER en el teclado en lugar del Button de búsqueda para activar la función de búsqueda.

Gracias por la ayuda de antemano.


Para evitar que el enfoque avance al siguiente campo editable (si tiene uno) es posible que desee ignorar los eventos clave, pero manejar eventos de activación. También prefiero filtrar primero en keyCode, suponiendo que sería marginalmente más eficiente. Por cierto, recuerda que regresar verdadero significa que has manejado el evento, por lo que ningún otro oyente lo hará. De todos modos, aquí está mi versión.

ETFind.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) { if (event.getAction() == KeyEvent.ACTION_DOWN) { // do nothing yet } else if (event.getAction() == KeyEvent.ACTION_UP) { findForward(); } // is there any other option here?... // Regardless of what we did above, // we do not want to propagate the Enter key up // since it was our task to handle it. return true; } else { // it is not an Enter key - let others handle the event return false; } } });


Puede ser que puedas agregar un atributo a tu EditText así:

android:imeOptions="actionSearch"


agrega un atributo a EditText como android: imeOptions = "actionSearch"

esta es la mejor manera de hacer la función

y las imeOpciones también tienen otros valores como "ir", "siguiente", "hecho", etc.


esta es una muestra de una de mi aplicación cómo manejo

//searching for the Edit Text in the view final EditText myEditText =(EditText)view.findViewById(R.id.myEditText); 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)) { //do something //true because you handle the event return true; } return false; } });


<EditText android:id="@+id/search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/search_hint" android:inputType="text" android:imeOptions="actionSend" />

Luego puede escuchar las pulsaciones del botón de acción definiendo un TextView.OnEditorActionListener para el elemento EditText. En su oyente, responda al ID de acción de IME apropiado definido en la clase EditorInfo, como IME_ACTION_SEND. Por ejemplo:

EditText editText = (EditText) findViewById(R.id.search); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { sendMessage(); handled = true; } return handled; } });

Fuente: https://developer.android.com/training/keyboard-input/style.html