Acton酒吧行动菜单 - SearchView

时间:2013-06-26 14:24:43

标签: android actionbarsherlock menuitem

我有一个搜索视图作为操作项。除了我想拦截搜索执行的事件之外,每件事情都运行正常。此时,将使用搜索结果启动新活动。当发生这种情况时,我想要折叠上一个活动中的搜索视图。因此,当用户退出搜索结果活动时,应该折叠操作菜单。

1 个答案:

答案 0 :(得分:0)

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar

        //Create the search view
        final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search");

        menu.add(Menu.NONE,Menu.NONE,1,"Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() > 0) {
                    // Search

                } else {
                    // Do something when there's no input
                }
                return false;
            }
            @Override
            public boolean onQueryTextSubmit(String query) { 

                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);

                Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
                setSupportProgressBarIndeterminateVisibility(true);

                Handler handler = new Handler(); 
                handler.postDelayed(new Runnable() { 
                     public void run() { 
                         setSupportProgressBarIndeterminateVisibility(false);
                     } 
                }, 2000);

                return false; }
        });

        return true;
    }

我认为您可以在SearchView方法中展开onQueryTextSubmit,请参阅上面的代码