Cómo cerrar Android Soft KeyBoard de forma programática?

Actualmente estoy mostrando softkeyboard usando el siguiente código

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN); 

Y aquí no enlazé el softkeyboard con Edittext debido a que había usado el código anterior.

Ahora quiero cerrar el SoftKeyboard por lo que actualmente estoy utilizando el código a continuación, pero no está funcionando.

 imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN); 

¿Puede alguien sugerirme qué utilizar para cerrar el softKeyboard?


Basado en Abajo Respuesta Quiero dejar claro que no estoy usando EditText, uso Layout en el que quiero mostrar Teclado y Ocultar teclado. Quiero enviar el evento clave del teclado al área remota bcoz de que no utilicé editText.

He probado y esto está funcionando:

 ... //to show soft keyboard imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); //to hide it, call the method again imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

Por cierto, el segundo parámetro de su código no es correcto, por favor eche un vistazo aquí .

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

Utilice este código de trabajo:

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

Si lo desea, puede utilizar toda la clase y llamar al método KeyboardUtil.hideKeyBoard (context) donde:

 public class KeyboardUtil { public static void hideKeyboard(Activity activity) { try { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } catch (Exception e) { // Ignore exceptions if any Log.e("KeyBoardUtil", e.toString(), e); } } } 

La respuesta de user942821 para ocultarlo funciona:

 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

Pero esto también funciona para mí para ocultarlo:

 imm.toggleSoftInput(0, 0); 

También puede intentar:

 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 

Cuando se utiliza '0' en el primer parámetro a veces el teclado se activa en los lugares equivocados en circunstancias extrañas que no he sido capaz de averiguar cómo duplicar todavía. Todavía estoy probando este último ejemplo, pero se actualizará cuando descubra más.

Consulte la página de documentación de toggleSoftInput para obtener más información.

Esto funciona bien

 InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0); 

También puedes probar

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

Aquí está la solución s, que comprueba si el teclado está visible

  public static void hideKeyboard(Activity activity) { if (isKeyboardVisible(activity)) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); } } public static boolean isKeyboardVisible(Activity activity) { ///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device Rect r = new Rect(); View contentView = activity.findViewById(android.R.id.content); contentView.getWindowVisibleDisplayFrame(r); int screenHeight = contentView.getRootView().getHeight(); int keypadHeight = screenHeight - r.bottom; return (keypadHeight > screenHeight * 0.15); } 
 InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

Para ocultar el teclado,

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

Aquí, "mView" puede ser cualquier vista que sea visible en la pantalla

Este código oculta el teclado desde dentro onItemClick de un AutoCompleteTextView

 public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) { // whatever your code does InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0); } 
 private void close() { this.requestHideSelf(0); } 

Este método es muy simple

  • Cambiar el color de fondo del teclado
  • Teclado con scrollView
  • Android: El cuadro de diálogo muestra el teclado virtual automáticamente cuando el foco está en un EditText que no funciona
  • Cambiar el perfil del teclado mediante programación
  • Cómo NO cerrar el teclado cuando se pulsa DONE en el teclado
  • Cómo mantener una barra de navegación inferior de ser empujado hacia arriba en el teclado mostrado
  • Desplácese hasta el teclado suave
  • Cómo ocultar el teclado virtual en Android después de hacer clic fuera de EditText?
  • Establecer el modo de teclado en el teclado personalizado android
  • Teclado Android InputType sin EditText
  • Cómo quitar las teclas repetibles, vista previa de clave del teclado personalizado de Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.