【问题标题】:Keyboard pops up after i dismiss DialogFragment我关闭 DialogFragment 后弹出键盘
【发布时间】:2019-02-08 11:47:36
【问题描述】:

在我的片段中,我调用了一个 DialogFragment,然后我调用了

getDialog().dismiss();

并在我的 onDismiss() 中有这个

@Override
public void onDismiss(DialogInterface dialog)
{

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

    super.onDismiss(dialog);
}

但之后由于某种原因,当我返回片段时似乎弹出了一个键盘,我尝试了各种方法,包括尝试在片段的回调中隐藏键盘,但似乎没有任何效果。

【问题讨论】:

    标签: android


    【解决方案1】:

    在我的片段中,我调用了DialogFragment,然后我调用了

    试试这个:

    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);
        }
    

    在您的代码中

    @Override
        public void onDismiss(DialogInterface dialog)
        {
            super.onDismiss(dialog);
            hideKeyboard(getActivity());
        }
    

    在清单文件中使用stateAlwaysHidden

        <activity
            android:screenOrientation="portrait"
            android:name=".chat.activity.ChatActivity"
            android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
    
        </activity>
    

    【讨论】:

    • 是的,谢谢我已经有了那个方法,我正在回调中尝试它,我在 onDismiss() 中尝试了它,正如您在编辑中建议的那样,但结果相同
    • @BoluOkunaiya 然后在清单文件中使用 stateAlwaysHidden
    【解决方案2】:

    因为您正在使用toggleSoftInput(int showFlags, int hideFlags) 来切换软键盘的状态。来自文档

    This method toggles the input method window display. 
    

    您可以使用hideSoftInputFromWindow() 强制Android 使用InputMethodManager 隐藏虚拟键盘,调用hideSoftInputFromWindow,传入包含焦点view 的窗口令牌。

    View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

    【讨论】:

      【解决方案3】:

      如果你在片段中试试这个代码块

      getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWaYS_HIDDEN);
      

      或者如果你在活动:

      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWaYS_HIDDEN);
      

      请确保在设置内容视图之前或在为视图膨胀之前这样做

      【讨论】:

        猜你喜欢
        • 2020-01-03
        • 1970-01-01
        • 2013-12-09
        • 2015-01-26
        • 1970-01-01
        • 2013-02-28
        • 2013-02-04
        • 1970-01-01
        • 2018-04-16
        相关资源
        最近更新 更多