android searchView在动作栏中如何使用它?

时间:2015-07-07 08:14:17

标签: android android-actionbar searchview

我在SearchView中有一个ActionBar。现在我想使用它,但我不知道如何使用它。在自定义SearchView中,我使用它:

    SearchView sear = (SearchView) findViewById(R.id.searchView);
sear.setOnQueryTextListener(new SearchView.OnQueryTextListener()

                                          {
                                              @Override
                                              public boolean onQueryTextSubmit (String query){
                                                  return false;
                                              }
                                              @Override
                                              public boolean onQueryTextChange (String newText){
                                                  listDataAdapter.getFilter().filter(newText);
                                                  return true;
                                              }
                                          }

        );

现在我在一个操作栏中有SearchView,我通过代码显示:

public class DataListActivity extends ActionBarActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_list_layout);
@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()));

        return true;

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                Intent intent = new Intent(getApplicationContext(),MainMenu.class);
                startActivity(intent);
                return true;
            case R.id.action_Exit:
                openExit();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void openExit() {
}

如何使用SearchView?我不知道如何在操作栏中使用SearchView

1 个答案:

答案 0 :(得分:0)

设置onQueryTextListener(..)类似于您在searchView上提到的问题。