简体   繁体   中英

Android Actionbar SearchView

I have tried the actionbar SearchView in android. Unable to get the entered string from the below code please anyone help?

在此处输入图片说明

try changing onQueryTextSubmit code to:

    public boolean onQueryTextSubmit(String query) {
        if (query == null) {
            Toast.makeText(getApplicationContext(), "Null Query", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Query is:" + ""+query, Toast.LENGTH_SHORT).show();
        }
    }

see this

You can use the search bar as like this.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
            .getActionView();
    if (null != searchView) {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            //Here u can get the value "query" which is entered in the search box.

        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return super.onCreateOptionsMenu(menu);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM