Android-如何在按下键盘的搜索按钮时隐藏键盘?

Jae*_*hak 0 keyboard android

按下键盘的搜索按钮时如何隐藏键盘?

当用户完成在edittext中的输入并按键盘上的“搜索按钮”时,键盘应处于隐藏状态。

Roh*_*5k2 5

在xlm中将imeOptions设置为“ actionSearch”以编辑文本

初始化您的监听器 EditText

searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }

        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

用户单击搜索时隐藏键盘。

private void performSearch() {
    searchEditText.clearFocus();
    InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    ... perform search ...
}
Run Code Online (Sandbox Code Playgroud)