Android OnKeyListener not registering Enter and Search keys.

懵懂的女人 提交于 2019-12-13 07:17:57

问题


I am using a Popup window and within that window I have a search view. Upon clicking the search view the soft keyboard comes on screen. So I want whenever I press the search button or enter button from keybaord it will get the data from the search view and will show the relevant information. I am using an OnKeyListener to get the key but it is not registering the enter and search key presses.

My code:

searchview.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View arg0, int arg1, KeyEvent arg2) {

            if(arg2.getKeyCode() == KeyEvent.KEYCODE_SEARCH || arg2.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                popupwindow.setFocusable(false);
                System.out.println("search pressed");
                Toast.makeText(getApplicationContext(), "Search Pressed", 0).show();    
            }
            return false;
        }
}); 

回答1:


Use arg2.getAction() instead of getKeyCode() and it should work.




回答2:


You want getKeyCode() not getAction().

Using an IME action listener would be another way to acheive this.




回答3:


The searchView has it's on callbacks from the keyboard.

Handle the search/enter in the setOnQueryTextListener. This listener has 2 callbacks:

  1. a onQueryTextSubmit
  2. and a onQueryTextChange

Both pick up the events from your keyboard.

This is part of my code (called in the onCreateOptionsMenu)

    MenuItem searchItem = menu.findItem(R.id.action_search);

    SearchManager searchManager = (SearchManager) SearchActivity.this.getSystemService(Context.SEARCH_SERVICE);

    if (searchItem != null) {
        final SearchView searchView = (SearchView) searchItem.getActionView();
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(SearchActivity.this.getComponentName()));
        }

        if (searchView == null) {
            return true;
        }

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
            // handle text submitted by user in here
                String text = query;
                searchView.setQuery("", false);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                if (text.length() > 0) downloadSearchQuery(text);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) { 
            // handle text changed here
                String mQueryString = newText.toString().trim();
                if (mQueryString.toString().trim().length() >= 3) {
                    downloadSearchQuery(mQueryString.toString().trim());
                } else {
                    if (mSearchList.size() != 0) {
                        mSearchList.clear();
                        mAdapter.notifyDataSetChanged();
                    }
                    checkAdapterIfEmpty();
                }
                return true;
            }
        });
    }


来源:https://stackoverflow.com/questions/14994634/android-onkeylistener-not-registering-enter-and-search-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!