【问题标题】:Hide the on-screen keyboard on button press按下按钮时隐藏屏幕键盘
【发布时间】:2014-04-24 11:01:21
【问题描述】:

我想知道在用户按下按钮后如何最好地隐藏屏幕(软?)键盘。当用户在提交之前输入文本时,他们的键盘可能仍然可见,我想缩小他们提交时的窗口。

我已找到问题herehere 的答案,但似乎都无法正常工作。尽管 Toast 通知显示在顶部,但它们的键盘仍然可见。

为了清楚起见,我的代码:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

我也试过了:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromInputMethod(((EditText)findViewById(R.id.txtToWrite)).getWindowToken(), InputMethodManager.0); 

【问题讨论】:

    标签: android keyboard android-softkeyboard android-input-method


    【解决方案1】:

    你试过 InputMethodManager.HIDE_NOT_ALWAYS 吗?

    InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    

    我认为您也可以尝试从 EditText/SearchView 中清除焦点。例如:

    searchBar = (SearchView) v.findViewById(R.id.ditto_search_bar);
    searchBar.clearFocus();
    

    【讨论】:

    • 是的,我发现了它并且它有效。感谢您的回复,对于迟到的接受我深表歉意。
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2018-02-18
    • 2023-04-02
    • 2011-07-30
    相关资源
    最近更新 更多