Pregunta

Cuando mi usuario presiona Ingresar En el virtual Android "Ingreso de validar el usuario!" teclado mi teclado Manténgase visible!(¿Por qué?)

Aquí mi código Java...

private void initTextField() {
    entryUser = (EditText) findViewById(R.id.studentEntrySalary);
    entryUser.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:
                        userValidateEntry();
                        return true;
                }
            }

          return true;
        }
    });
}

private void userValidateEntry() {
    System.out.println("user validate entry!");
}

...aquí mi vista

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 </LinearLayout>

¿Quizás algo anda mal en mi dispositivo virtual?

¿Fue útil?

Solución

Esto debe hacerlo:

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                // NOTE: In the author's example, he uses an identifier
                // called searchBar. If setting this code on your EditText
                // then use v.getWindowToken() as a reference to your 
                // EditText is passed into this callback as a TextView

                in.hideSoftInputFromWindow(searchBar
                        .getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
               userValidateEntry();
               // Must return true here to consume event
               return true;

            }
            return false;
        }
    });

Otros consejos

Mantener el SingleLine = "true" y añadir imeOptions = "actionDone" al EditarTexto. Luego, en el OnEditorActionListener comprobar si ActionID == EditorInfo.IME_ACTION_DONE, al igual que (pero cambiarlo a su aplicación):

if (actionId == EditorInfo.IME_ACTION_DONE) {

                if ((username.getText().toString().length() > 0)
                        && (password.getText().toString().length() > 0)) {
                    // Perform action on key press
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(username.getWindowToken(),
                            0);
                    doLogin();
                }
            }

Si usted hace la caja de texto de una sola línea (creo que la estructura está llamado SingleLine a los archivos del formato XML) saldrá fuera del teclado en entrar.

Aquí van: http://developer.android.com/ referencia / android / R.styleable.html # TextView_singleLine

Soy crear un componente personalizado que se extiende AutoCompleteTextView, como en el siguiente ejemplo:

public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
        InputMethodManager inputManager =
                (InputMethodManager) getContext().
                        getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(
                this.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
    return super.onKeyPreIme(keyCode, event);
}

Estoy utilizando este código en el AlertDialog.Builder, pero es posible estar utilizando a la actividad.

sólo tiene que añadir esta línea en su edición de texto.

android:imeOptions="actionDone"'

se puede especificar el siguiente ID de edición de texto para mover a la edición de texto al hacer clic con el botón del teclado hecho.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top