android searchView在actionbar过滤器问题

时间:2015-07-07 10:37:05

标签: android searchview

我在操作栏中有searchview它无法正常工作我尝试这样做

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.menu_main, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    android.support.v7.widget.SearchView searchView =
            (android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);
    return true;
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){


        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

        });

}

但它给我一个错误,即searchview中的android.support.v7.widget.SearchViewOnQueryTextListener无法应用

1 个答案:

答案 0 :(得分:0)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.menu_main, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    android.support.v7.widget.SearchView searchView =
            (android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){


        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
        });
return true;
}
  1. 回归应该在最后,
  2. 最后三个括号不正确。