单击软键盘上的“搜索”按钮时,搜索视图消失

时间:2018-07-12 06:20:16

标签: android

我的菜单上有一个搜索视图。当我搜索某些内容,然后单击手机键盘上的“搜索”按钮时,搜索视图就会消失。

我尝试实现setOnKeyListener,但似乎不起作用。帮助将不胜感激。

这是我的代码:

searchView.setOnKeyListener(new View.OnKeyListener() {    
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            switch (keyCode){
                case KeyEvent.KEYCODE_SEARCH:
                    String search_text = searchView.getQuery().toString().trim();
                    int s = 0;
                    if (adapter != null) {
                        s = adapter.ManageProducts_filter(search_text, newmSectionList);
                    }
                    if (s != 0) {
                        searchView.setVisibility(View.GONE);
                    }
                    return true;
             }
        }
        return false;
    }
});

还实现了setOnQueryTextListener:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        String text = query.toString().toLowerCase().trim();
        int size = 0;
        if (adapter != null) {
            size = adapter.ManageProducts_filter(text, newmSectionList);
        }
        if (size != 0) {
            searchView.setVisibility(View.GONE);
        }
        return false;
    }
    @Override
    public boolean onQueryTextChange(String newText) {
        if (adapter != null) {
            adapter.ManageProducts_filter(newText, newmSectionList);
        }
        return false;
    }
});
return super.onCreateOptionsMenu(menu);

0 个答案:

没有答案