Comment fermer Android Soft KeyBoard programmatically?
je montre actuellement softkeyboard en utilisant le code suivant
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
et ici je n'ai pas lier le softkeyboard avec Edittext à cause de cela j'avais utilisé le code ci-dessus.
maintenant je veux fermer le SoftKeyboard donc j'utilise actuellement le code ci-dessous mais il ne fonctionne pas.
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
Quelqu'un peut-il me suggérer quoi utiliser pour fermer le softKeyboard ?
basé sur la réponse ci-dessous je veux vous faire comprendre que je n'utilise pas EditText, j'utilise Layout sur lequel je veux montrer le clavier et cacher le clavier. Je veux envoyer l'événement de touche de clavier à la zone distante bcoz de ce que je n'ai pas utilisé editText.
13 réponses
j'ai testé et cela fonctionne:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
soit dit en passant, le deuxième paramètre de votre code n'est pas bon, s'il vous plaît jetez un oeil à ici .
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
utiliser ce code de travail:
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
si vous voulez, vous pouvez utiliser toute la classe et appeler KeyboardUtil.méthode hideKeyBoard (context) partout:
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);
}
}
}
user942821 réponse pour cacher cela fonctionne:
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Mais cela fonctionne aussi pour moi de le cacher:
imm.toggleSoftInput(0, 0);
Vous pourriez aussi essayer de l':
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
Lorsqu'on utilise "0" dans le premier paramètre, il arrive que le clavier bascule au mauvais endroit dans des circonstances bizarres que je n'ai pas encore réussi à comprendre comment dupliquer. Je suis toujours tester ce dernier exemple, mais mettra à jour quand je découvre plus.
Voir toggleSoftInput page de documentation pour plus d'informations.
Cela fonctionne bien
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
Fermer cacher/Android Clavier Souple
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
it's working for me i hope it's work for you..
Ouvrir Android Clavier Souple
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
, Vous pouvez également essayer de
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Voici la solution, qui vérifie si le clavier 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);
Pour masquer le clavier, la
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
ici, "mView" peut être n'importe quelle vue visible à l'écran
ce code cache le clavier de l'intérieur onItemClick
d'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);
}
cette méthode est très simple