Android descartar teclado

¿Cómo descargo el teclado cuando se presiona un botón?

¿Desea desactivar o deshabilitar un teclado virtual?

Si desea simplemente descartarlo puede utilizar las siguientes líneas de código en el botón de su clic en Evento

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

La solución anterior no funciona para todos los dispositivos y además utiliza EditText como parámetro. Esta es mi solución, simplemente llame a este método simple:

 private void hideSoftKeyBoard() { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); if(imm.isAcceptingText()) { // verify if the soft keyboard is open imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } } 

Esta es mi solución

 public static void hideKeyboard(Activity activity) { View v = activity.getWindow().getCurrentFocus(); if (v != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } 

También puede utilizar este código en el botón haga clic en evento

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

La primera solución con InputMethodManager funcionó como un campeón para mí, el método getWindow (). SetSoftInputMode no en android 4.0.3 HTC Amaze.

@Ethan Allen, no necesitaba hacer el texto de edición final. Tal vez usted está utilizando una clase interna de EditText que declaró el método que contiene? Podría convertir el EditText en una variable de clase de la Actividad. O simplemente declarar un nuevo EditText dentro de la clase interna / método y utilizar findViewById () de nuevo. Además, no encontré que necesitaba saber qué EditarTexto en el formulario tenía foco. Podría escoger uno arbitrariamente y usarlo. Al igual que:

  EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
  public static void hideSoftInput(Activity activity) { try { if (activity == null || activity.isFinishing()) return; Window window = activity.getWindow(); if (window == null) return; View view = window.getCurrentFocus(); if (view == null) return; InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null || !imm.isActive()) return; imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } catch (Throwable e) { e.printStackTrace(); } } 

Aquí está una solución de Kotlin (mezclando las varias respuestas en hilo)

Cree una función de extensión (quizás en una clase común de ViewHelpers)

 fun Activity.dismissKeyboard() { val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager if( inputMethodManager.isAcceptingText ) inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0) } 

Entonces simplemente consuma usando:

 // from activity this.dismissKeyboard() // from fragment activity.dismissKeyboard() 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.