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.

45
demandé sur Cœur 2012-01-09 11:12:51

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 .

88
répondu 2012-01-09 08:14:21
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
39
répondu Jana 2012-01-09 07:29:24

utiliser ce code de travail:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
30
répondu Reddy Raaz 2012-10-03 13:26:43

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);
        }
    }
}
9
répondu Reddy Raaz 2012-10-03 08:51:07

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.

2
répondu uowaep 2013-12-26 00:50:11

Cela fonctionne bien

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
2
répondu Daniel Cherubini 2015-05-22 10:55:19

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);
        }
2
répondu Ashutosh Srivastava 2017-09-16 07:48:57

, Vous pouvez également essayer de

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
0
répondu mseo 2012-01-09 07:44:28

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);
    }
0
répondu Boris Treukhov 2016-08-24 23:01:13
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
0
répondu Priyanka 2016-09-20 05:57:23

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

0
répondu varotariya vajsi 2016-10-22 11:56:29

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);
}
0
répondu tony gil 2016-12-30 18:58:47
private void close() {
    this.requestHideSelf(0);
}

cette méthode est très simple

-2
répondu Vincent Williams 2015-03-29 19:57:34