1

Donc, je trouve ce morceau de code vraiment cool pour fermer le clavier lorsque je clique sur non EditText vues. Et cela fonctionne très bien, sauf pour Fragments et DialogFragments ont commencé à utiliser getChildFragmentManager(). Est-ce que quelqu'un pourrait expliquer pourquoi l'exception et comment je pourrais y remédier? Je conserve le code dans une classe utilitaire et l'utilise dans toute mon application. Essentiellement, pour le cas de problème, je l'utilise sur FragmentB est commencé par FragmentA en utilisant getChildFragmentManager(), puis le code n'affecte pas les vues de FragmentB.Comment faire pour fermer le clavier en cliquant sur des vues non-EditText dans le fragment enfant

+0

Quelle est l'exception? Pouvez-vous partager les journaux? –

+0

@mussharapp par 'exception', je veux dire aberration: je ne parle pas d'une exception Java. – learner

+0

Qu'est-ce que l'activité? où vous avez initialisé? –

Répondre

0

essayez ceci pour masquer le clavier,

public void hideSoftKeyboard() { 
     try { 
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
      inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
0

essayez le code ci-dessous:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    return true; 
} 
0

Si vous utilisez FragmentActivity utilisation de fragments non activité. Je suggère d'essayer essayer.

comme

try{ 
InputMethodManager inputMethodManager = (InputMethodManager) activity 
     .getSystemService(Activity.INPUT_METHOD_SERVICE); 
}catch(Exception){ 
InputMethodManager inputMethodManager = (InputMethodManager) fragmentactivity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
} 
0

J'ai trouvé la solution. Peut-être que quelqu'un d'autre trouvera utile

public static void setupUI(View view, final Activity activity) { 

    // Set up touch listener for non-text box views to hide keyboard. 
    if (!(view instanceof EditText)) { 

     view.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       LayoutUtils.hideSoftKeyboard(activity,v); 
       return false; 
      } 

     }); 
    } 

    // If a layout container, iterate over children and seed recursion. 
    if (view instanceof ViewGroup) { 

     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

      View innerView = ((ViewGroup) view).getChildAt(i); 

      setupUI(innerView, activity); 
     } 
    } 
} 

private static void hideSoftKeyboard(Activity activity, View v) { 
    InputMethodManager inputMethodManager = (InputMethodManager) activity 
     .getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
} 

Voyez comment je remplacerai activity.getCurrentFocus() avec v. Et c'est tout. Cela fonctionne dans toutes les situations maintenant.

Questions connexes