单击片段中的导航抽屉时隐藏android软键盘

时间:2017-03-17 04:59:50

标签: android

我想在FragmentDrawer中点击汉堡图标时隐藏软键盘。我在Fragment中使用ActionBarDrawerToggle。当我在onDrawerOpened和onDrawerClosed中使用输入法管理器时,它显示错误getSystemService和getCurrentFocus方法无法解析。

2 个答案:

答案 0 :(得分:1)

设置" Id"主要布局。并且在" OnClickListener"调用粘贴在下面的方法。

public static void hideKeyboard(Activity activity) {
        if (activity != null) {
            if (activity.getCurrentFocus() != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity
                        .INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
                        .getWindowToken(), 0);
            }
        }
    }

答案 1 :(得分:0)

将Activity用于getCurrentFocus

      @Override
      public void onDrawerStateChanged(int newState) {               
        InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
        InputMethodManager.HIDE_NOT_ALWAYS); 
        }
相关问题