简体   繁体   中英

SearchView: go to another layout as enter is pressed in Android

I'm trying to launch a query to retrieve data from an online database, but I can't understand how, with the SearchView, go to another layout as a key (in this case enter) is pressed. I thought that maybe it has, like the button, a clickListener that when clicked (after setting the mobile_navigation.xml) you can set in which layout go, but there's not. Here's my code:

Context myContext = this;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Insert here the title");

searchView.setOnQueryTextListener(new OnQueryTextListener() {

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

    @Override
    public boolean onQueryTextSubmit(String query) {
        query = query.replace(' ', '+');
        Log.d("Query", query);
        //Navigation.createNavigateOnClickListener(R.id.action_nav_home_to_searchFragment, null);
        ComponentName cm = new ComponentName(myContext, SearchFragment.class);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(cm));
        return false;
    }

});

return true;
}

You can't use createNavigateOnClickListener on the onQueryTextSubmit because the enter button is already clicked. But you can use it using setOnSearchClickListener instead of setOnQueryTextListener

searchView.setOnSearchClickListener(Navigation.createNavigateOnClickListener(R.id.btnLogin, null))

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