【问题标题】:Hide keyboard when navigating from a fragment to another从片段导航到另一个片段时隐藏键盘
【发布时间】:2015-01-10 18:01:40
【问题描述】:

我有一个包含编辑文本的片段。按下编辑文本时,将显示键盘。当按下右上角的保存按钮时,应用程序返回到上一个片段,但键盘仍然存在。

我希望在导航到上一个片段时隐藏键盘。

请注意,我尝试了以下解决方案: Close/hide the Android Soft Keyboard.

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);

我尝试在 onCreate 方法中的两个片段中使用它。

我也尝试在布局中隐藏软键盘:

android:windowSoftInputMode="stateAlwaysHidden"

不幸的是,这些都不起作用。

我会发布一些图片,但我还没有足够的声誉。 我会感谢任何建设性的帮助和意见,并且不要忘记“智者可以从愚蠢的问题中学到的东西比傻瓜可以从明智的答案中学到的更多”。 :)

问候, 亚历山德拉

【问题讨论】:

  • 是一个普通的编辑文本。就像在解决方案中一样。
  • 尝试使用 getView()(来自 Fragment 的方法)

标签: java android kotlin android-fragments android-softkeyboard


【解决方案1】:

将隐藏键盘的代码放在你的“保存按钮”点击监听器中,并使用此方法隐藏键盘:

    public static void hideKeyboard(Activity activity) {
        InputMethodManager inputManager = (InputMethodManager) activity
        .getSystemService(Context.INPUT_METHOD_SERVICE);

        // check if no view has focus:
         View currentFocusedView = activity.getCurrentFocus();
         if (currentFocusedView != null) {
             inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
         }
     }

【讨论】:

  • 将该代码放在一个公共类中,然后您可以从任何片段需要它的一行中调用它。好东西 - 多年来一直在寻找这个
  • 最佳答案(y)
【解决方案2】:

科特林

对于 Kotlin,您可以将其用作顶级函数,只需将代码添加到单独的类中,例如 Utils.kt

fun hideKeyboard(activity: Activity) {
    val inputMethodManager =
        activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

    // Check if no view has focus
    val currentFocusedView = activity.currentFocus
    currentFocusedView?.let {
        inputMethodManager.hideSoftInputFromWindow(
            currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}

要从 Fragment 访问它,可以这样称呼它:

hideKeyboard(activity as YourActivity)

感谢 Silvia H 提供 Java 代码。

【讨论】:

  • 可以是activity.currentFocus?.let { currentFocusedView ->
【解决方案3】:

在片段或活动中隐藏键盘的最简单方法

解决方案:1

//hide keyboard
public static void hideKeyboard(Context ctx) {
    InputMethodManager inputManager = (InputMethodManager) ctx
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    // check if no view has focus:
    View v = ((Activity) ctx).getCurrentFocus();
    if (v == null)
        return;

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

解决方案:2

    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

【讨论】:

    【解决方案4】:
    @Override
        public void onDestroyView() {
            super.onDestroyView();
            View view = getActivity().getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    

    【讨论】:

      【解决方案5】:
        public void hideKeyboard(Activity activity) {
              InputMethodManager inputManager = (InputMethodManager) activity
              .getSystemService(Context.INPUT_METHOD_SERVICE);
      
              // check if no view has focus:
               View currentFocusedView = activity.getCurrentFocus();
               if (currentFocusedView != null) {
                   inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
               }
           }
      

      【讨论】:

        【解决方案6】:

        您可以通过将此代码放在您要离开的片段的 onPause 中来隐藏键盘。

            @Override
            public void onPause() {
            super.onPause();
            InputMethodManager imm = (InputMethodManager) 
            requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-12-16
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          • 2021-05-02
          • 1970-01-01
          • 2013-05-12
          • 2022-01-02
          相关资源
          最近更新 更多