【问题标题】:Keyboard not closing when dismiss the dialogFragment关闭 dialogFragment 时键盘未关闭
【发布时间】:2020-01-03 09:29:17
【问题描述】:

我有一个片段用于显示地图。从这个片段中,我打开另一个具有editText 的对话框片段。单击editText时,键盘会打开,但是当我在没有先关闭键盘的情况下关闭dialogFragment时,dialogFragment会按原样关闭,但键盘仍保持打开状态。再次触摸键盘关闭的任何位置后。如何在关闭 dialogFragment 时关闭键盘。

我已经尝试过: android:windowSoftInputMode="stateAlwaysHidden" 在活动中。

也试过了:

InputMethodManager imm =
                (InputMethodManager) messageEditTxt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive())
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

在 onDismiss 函数中。

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    我也遇到了同样的问题,发现和manifest中定义的windowSoftInput有关。从活动中删除android:windowSoftInputMode="stateHidden" 后,它起作用了。

    【讨论】:

      【解决方案2】:

      试试这个

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

      用法

      hideSoftKeyboard(getActivity(), getView())
      

      【讨论】:

        【解决方案3】:

        只要把它放在你的全局类中,你就可以在任何地方访问

        public static void hideKeyboard(Activity activity) {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                //Find the currently focused view, so we can grab the correct window token from it.
                View view = activity.getCurrentFocus();
                //If no view currently has focus, create a new one, just so we can grab a window token from it
                if (view == null) {
                    view = new View(activity);
                }
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        

        对于活动-:

        GlobalClass.hideSoftKeyborad(MainActivity.this);
        

        对于片段-

        GlobalClass.hideSoftKeyborad(getActivity);
        

        【讨论】:

        • 已经尝试不工作......我通过了活动上下文,但没有解决方案。
        猜你喜欢
        • 2013-12-09
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 2022-11-18
        相关资源
        最近更新 更多