Comment fermez-vous / cachez le clavier Android soft programmatically

Mots clés : androidandroid-edittextandroid-softkeyboardandroid-input-methodsoft-keyboardandroid

meilleur 5 Réponses Comment fermez-vous / cachez le clavier Android soft programmatically

vote vote

100

// Check if no view has focus: View view = this.getCurrentFocus(); if (view != null) {       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 
// Only runs if there is a view that is currently focused this.currentFocus?.let { view ->     val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager     imm?.hideSoftInputFromWindow(view.windowToken, 0) } 
vote vote

86

public static void hideKeyboard(Activity activity) {     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);     //Find the currently focused view, so we can grab the correct window token from it.     View view = activity.getCurrentFocus();     //If no view currently has focus, create a new one, just so we can grab a window token from it     if (view == null) {         view = new View(activity);     }     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 
hideKeyboard(getActivity()); //won't work 
public static void hideKeyboardFrom(Context context, View view) {     InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 
<activity     android:name=".MyActivity"     android:windowSoftInputMode="stateAlwaysHidden"/> 
view = getView().getRootView().getWindowToken(); 
view = fragment.getView().getRootView().getWindowToken(); 
view = findViewById(android.R.id.content).getRootView().getWindowToken(); 
vote vote

71

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

63

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 
vote vote

53

tabHost.setOnTabChangedListener(new OnTabChangeListener() {     public void onTabChanged(String tabId) {         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);         imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);     } } 

Questions similaires