在SherlockActivity中按下搜索按钮时关闭软键盘

时间:2014-02-25 14:12:22

标签: android search actionbarsherlock menuitem

我有一个MenuItem searchItem。

按预期过滤listview结果。

但是当我按下软键盘键盘上的搜索按钮(放大镜)时,不会隐藏自己。

点击键盘上的搜索按钮,我该怎么做才能隐藏键盘

MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.menu_search);

        SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setSubmitButtonEnabled(true);
        searchView.setIconified(true);

  SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            @Override
            public boolean onQueryTextChange(String newText) 
            {
                // this is adapter that will be filtered
                if(companyListAdapter != null)
                {
                    companyListAdapter.getFilter().filter(newText);
                }

                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String newText) 
            {

                // this is adapter that will be filtered
                if(companyListAdapter != null)
                {
                    companyListAdapter.getFilter().filter(newText);
                }

                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

请帮忙。

1 个答案:

答案 0 :(得分:0)

这是隐藏和显示键盘的方法:

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}