teclado samsung pulsar ocultar fuera edittext desactivar como cerrar bloquear android virtual-keyboard

samsung - ocultar teclado android edittext



Cerrar el teclado virtual al presionar un botón (13)

Tengo una Activity con un EditText , un botón y un ListView . El propósito es escribir una pantalla de búsqueda en EditText , presionar el botón y hacer que los resultados de búsqueda llenen esta lista.

Todo esto funciona perfectamente, pero el teclado virtual se está comportando de manera extraña.

Si hago clic en EditText , obtengo el teclado virtual. Si hago clic en el botón "Listo" en el teclado virtual, desaparece. Sin embargo, si hago clic en mi botón de búsqueda antes de hacer clic en "Hecho" en el teclado virtual, el teclado virtual se mantiene y no puedo deshacerme de él. Al hacer clic en el botón "Listo" no se cierra el teclado. Cambia el botón "Hecho" de "Hecho" a una flecha y permanece visible.

Gracias por tu ayuda


Agregue el siguiente código dentro de su evento de clic de botón:

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


Como solo tiene un texto de edición, solo tiene que hacer clic en acción para ese texto de edición dentro de su clic y el resto lo maneja el sistema. Si tenía más de un texto de edición, esto no sería tan eficiente porque primero debe obtener el texto de edición enfocado. Pero en tu caso, funcionará perfectamente

myedittext.onEditorAction(EditorInfo.IME_ACTION_DONE)


Crash Null Point Exception Fix: tuve un caso en el que el teclado no se puede abrir cuando el usuario hace clic en el botón. Debes escribir una instrucción if para verificar que getCurrentFocus () no sea un nulo:

InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if(getCurrentFocus() != null) { inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


Debe implementar OnEditorActionListener para su EditView

public void performClickOnDone(EditView editView, final View button){ textView.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(EditView v, int actionId, KeyEvent event) { hideKeyboard(); button.requestFocus(); button.performClick(); return true; } });

Y ocultas el teclado por:

public void hideKeybord(View view) { inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); }

También deberías disparar el teclado oculto en tu botón usando onClickListener

Ahora, al hacer clic en "Listo" en el teclado y botón virtual, se hará lo mismo: ocultar el teclado y realizar una acción de clic.


Esta solución funciona perfecta para mí:

private void showKeyboard(EditText editText) { editText.requestFocus(); editText.setFocusableInTouchMode(true); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN); editText.setSelection(editText.getText().length()); } private void closeKeyboard() { InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); }


Para la actividad,

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

Para fragmentos, use getActivity ()

getActivity (). getSystemService ();

getActivity (). getCurrentFocus ();

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);


Prueba esto...

  1. Para mostrar el teclado

    editText.requestFocus(); InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

  2. Para ocultar el teclado

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);


Si configuras android:singleLine="true" , automáticamente el botón oculta el teclado.


Utiliza este código en su evento de clic de botón

// Check if no view has focus: View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }


Use el código a continuación

your_button_id.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } catch (Exception e) { } } });


InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Puse esto justo después del evento onClick(View v) .

Necesita importar android.view.inputmethod.InputMethodManager ;

El teclado se esconde cuando haces clic en el botón.


View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}


mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // hide virtual keyboard InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); return true; } return false; } });