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

时间:2015-01-25 02:15:34

标签: android keyboard

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

当用户输入edittext并按键盘的“搜索按钮”时,应该隐藏键盘..

1 个答案:

答案 0 :(得分:4)

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

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

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