4

我的SearchView操作栏中现在有一个它应该完全正常工作,无论如何我有一个问题,当我单击提交按钮时,即提交查询并显示结果,当我返回到我开始搜索的活动时从SearchView扩展中,我的问题是如何关闭它,即在提交时将其图标化。这是我的代码。

searchView = (SearchView) item.getActionView();
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(true);
            searchView.setIconified(true);
            searchView.setQueryRefinementEnabled(true);
            searchView
                    .setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            try {
                                  searchView = (SearchView) item.getActionView();
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setIconified(true);
        searchView.setQueryRefinementEnabled(true);
        searchView
                .setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        try {

                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                }); // doesnot work 
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            // TODO Auto-generated method stub
                            return false;
                        }
                    });
4

3 回答 3

2

对于 API>=14,您可以使用item.collapseActionView().

于 2013-02-06T15:27:21.040 回答
2

此解决方案有效:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.home_screen, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
        final SearchView searchView = (SearchView) searchMenuItem
                .getActionView();
        searchView.setIconifiedByDefault(false);
        if (searchManager != null && searchView != null) {
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));

            searchView
                    .setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {

                            if (!hasFocus) {
                                if (searchMenuItem != null) {
                                    searchMenuItem.collapseActionView();
                                }// end if
                                if (searchView != null) {
                                    searchView.setQuery("", false);

                                }// end if
                            }// end if

                        }
                    });

            searchView
                    .setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            /**
                             * hides and then unhides search tab to make sure
                             * keyboard disappears when query is submitted
                             */
                            if (searchView != null) {
                                searchView.setVisibility(View.INVISIBLE);
                                searchView.setVisibility(View.VISIBLE);

                            }
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            // TODO Auto-generated method stub
                            return false;
                        }
                    });

        }

        return super.onCreateOptionsMenu(menu);
    }
于 2013-10-06T18:03:13.687 回答
0

我从另一个问题中得到了这个,它似乎对我有用。

 @Override
 public void onStart()
 {
   if (mSearchView != null)
   {

       getSearchMenuItem().collapseActionView();
   }
 }
 @Override
public boolean onCreateOptionsMenu(Menu menu)
{
  //get your search view
  msearchMenuItem = menu.findItem(R.id.search);
}
public MenuItem getSearchMenuItem() {
             return msearchMenuItem;
}

每次点击后退按钮时它都会关闭搜索视图(这会使活动进入 onStart())

于 2013-07-23T13:42:40.963 回答