Comment fermer le clavier lorsqu'un bouton est enfoncé?
Comment fermer le clavier lorsqu'un bouton est enfoncé?
Réponses:
Vous souhaitez désactiver ou ignorer un clavier virtuel?
Si vous voulez simplement le rejeter, vous pouvez utiliser les lignes de code suivantes dans votre bouton en cliquant sur Événement
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity()) getCurrentFocus().getWindowToken()pour le premier argument hideSoftInputFromWindow(). Aussi, faites-le dans le onPause()et non le onStop()si vous essayez de le faire disparaître lorsque vous changez d'activité.
La solution ci-dessus ne fonctionne pas pour tous les appareils et de plus, elle utilise EditText comme paramètre. C'est ma solution, appelez simplement cette méthode 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);
}
}
isAcceptingText()a rendu cette réponse meilleure que d'autres
C'est ma solution
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);
}
}
Voici une solution Kotlin (mélange des différentes réponses dans le fil)
Créer une fonction d'extension (peut-être dans une classe ViewHelpers commune)
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
Ensuite, consommez simplement en utilisant:
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
La première solution avec InputMethodManager a fonctionné comme un champion pour moi, la méthode getWindow (). SetSoftInputMode ne fonctionnait pas sur Android 4.0.3 HTC Amaze.
@Ethan Allen, je n'avais pas besoin de rendre le texte d'édition définitif. Peut-être que vous utilisez une classe interne EditText que vous avez déclarée la méthode contenant? Vous pouvez faire de EditText une variable de classe de l'activité. Ou déclarez simplement un nouveau EditText dans la classe / méthode interne et utilisez à nouveau findViewById (). De plus, je n'ai pas trouvé que j'avais besoin de savoir quel EditText dans le formulaire avait le focus. Je pourrais simplement en choisir un arbitrairement et l'utiliser. Ainsi:
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();
//give decorView a chance
if (view == null) view = window.getDecorView();
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();
}
}
Cette solution s'assure qu'il cache le clavier et ne fait rien s'il n'est pas ouvert. Il utilise l'extension afin de pouvoir être utilisé à partir de n'importe quelle classe de propriétaire de contexte.
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
En utilisant le contexte de la vue, nous pouvons obtenir le résultat souhaité avec les méthodes d'extension suivantes dans Kotlin:
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
Une fois ceux-ci en place, il vous suffit d'appeler:
editTextFoo.dismiss()