在SearchView上强制图标化

时间:2012-11-11 02:59:20

标签: android actionbarsherlock searchview

我的操作栏中有一个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;
                        }
                    });

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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);
    }

答案 2 :(得分: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())