在片段中隐藏键盘

Lal*_*iya 6 keyboard android

我试图在活动中的片段内隐藏键盘时遇到这些错误:

错误:无法解析getSystemService

无法解析Context

无法解析getCurrentFocus()

 InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
 InputMethodManager.HIDE_NOT_ALWAYS);
Run Code Online (Sandbox Code Playgroud)

小智 12

在片段内你应该使用getActivity(),

        InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);
Run Code Online (Sandbox Code Playgroud)