如何在actionbar sherlock中添加搜索

时间:2013-02-20 17:18:04

标签: android search android-intent actionbarsherlock

我正在使用操作栏sherlock。现在我想向actionbar添加一个搜索选项,并将Edittext搜索到其他活动。

谁能帮助我吗?

1 个答案:

答案 0 :(得分:5)

做这样的事情

@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;
}