1

我正在使用操作栏夏洛克。现在我想将搜索选项添加到操作栏并将 Edittext 搜索意图添加到其他活动。

谁能帮我?

4

1 回答 1

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;
}
于 2013-02-25T18:40:22.097 回答