0

我想从Spinner中选择一个项目后尝试隐藏键盘,但代码无效并且没有任何反应。但在另一方面,相同的代码在普通片段中工作。在DialogFragment中从微调框中选择项目时隐藏键盘

这里是隐藏键盘的方法:

public static void hideKeypad(Activity activity) { 
    View view = activity.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
} 
+0

### https://stackoverflow.com/questions/18447063/spinner-get-state-or-get-notified-when-opens – Mallikarjuna

回答

0

你需要指定FragmentrootView因为在你的方法是看为我工作getCurrentFocus() == null,所以它永远不会去其余的代码。

这是正确的代码:

public static void hideKeypad(Activity activity, View view) { 
if (view != null) { 
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
} 

使类变量View并与FragmentonCreateView()rootView等于它在这Fragment任何地方使用此方法。

+0

谢谢,它与我合作。 –

0

这在片段

public void removePhoneKeypad() { 
    if(getActivity().getCurrentFocus()!=null &&getActivity().getCurrentFocus().getWindowToken() != null) { 
     System.out.println("getCurrentFocus() in frag"); 
     InputMethodManager inputManager = (InputMethodManager) rootView 
       .getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 

     IBinder binder = getActivity().getCurrentFocus().getWindowToken(); 
     inputManager.hideSoftInputFromWindow(binder, 
       InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
    getActivity().getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
} 
+0

但它不能在DialogFragment .. –

+0

ohh。我也面临着关闭键盘的问题。它正在工作的大多数场景。 –

相关问题